是否可以將標籤內的文本與“大”框架垂直對齊 (Is it possible to vertically align text inside labels with a "large" frame)


問題描述

是否可以將標籤內的文本與“大”框架垂直對齊 (Is it possible to vertically align text inside labels with a "large" frame)

In my application I have multiple tableviews with custom cells. Some of the text in the cells are spread out on between 2‑4 lines so the height of the label is large enough to contain the content. 

However on iPad the screen is larger and I want the text to appear at the top left point of the label, not in the middle as it do when you use numberOfLines property. I know you can horizontally align the text, but it must be possible to align the text to the top left also? Or is it impossible. 

Screenshot:

‑‑‑‑‑

參考解法

方法 1:

Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.

As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.

方法 2:

I actually noticed that using

[Blue setSizeToFit]

does align vertically to the top. The default being centered.

方法 3:

sizeToFit does not work in UITableCellView, because the Cells are reused during scrolling.

The solution is to calculate the table cell height according to the font and font size and adjust the label height to it.

As it was quite difficult to find the solution on the web I wrote a short post about it

方法 4:

You can do it as follows

  1. Set your label's numberOfLines property 0 from IB

  2. Set your label's lineBreakMode as UILineBreakModeWordWrap (very very important)

now whatever you set on the label just append few @"\n" to it..... ex.‑

[yourTextLabel setText:@"myLabel\n\n\n\n\n"];

方法 5:

In iOS 7 sizeWithFont: is now deprecated!  Several solutions like subclassing UILabel have to be adapted.

My solution for top aligned label text: In a subclass TopVerticalAlignmentLabel : UILabel override drawRect: as follows:

‑ (void)drawRect:(CGRect)rect
{
    CGRect labelStringRect = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX)
                                                     options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                  attributes:@{ NSFontAttributeName: self.font, /* further attributes */}
                                                     context:nil];

    [super drawTextInRect:CGRectMake(0, 0, self.frame.size.width, labelStringRect.size.height)];
}

(by LuckyLukeEmptyStacknembletonHonsFahim ParkarAppsolutEinfach)

參考文件

  1. Is it possible to vertically align text inside labels with a "large" frame (CC BY‑SA 3.0/4.0)

#uilabel #objective-c #iphone #iOS #uitableview






相關問題

標籤邊框內的間距 (spacing inside label's border)

awakeFromNib 無法從 UILabel 獲取值,也無法禁用 UIButton (awakeFromNib can't get value from UILabel and can't disable UIButton)

如何在 UILabel 中製作陰影 (How to make shadow in UILabel)

如何計算 UILabel 的字體大小 (How can I calculate font size of UILabel)

如何在情節提要中為uilabel設置大於300的字體大小? (How to set font size greater than 300 for uilabel in storyboard?)

隨著 UILabel 尺寸的增加,在我的 UILabel 周圍獲得額外的空間 (Getting extra space around my UILabel as it increases in size)

UILabel 不顯示所有內容 (UILabel not displaying everything)

在 UIView 中將標籤居中 (Centering a label in a UIView)

UITableViewCell 未在其中顯示 UILabel (UITableViewCell not showing UILabel within)

帶有 UIMenucontroller 的 UILabel 不會通過外部觸摸退出第一響應者 (UILabel with UIMenucontroller not resigning first responder with touch outside)

UILabel 動態高度問題 (UILabel Dynamic height problem)

是否可以將標籤內的文本與“大”框架垂直對齊 (Is it possible to vertically align text inside labels with a "large" frame)







留言討論