如何做一个UILabel的minimumScaleFactor工作

&&&&UILabel&*label&=&[[UILabel&alloc]&initWithFrame:CGRectMake(10,&30,&300,&260)];&&
&&&&label.text&=&@&Label&Text &;
&&&&label.font&=&[UIFont&systemFontOfSize:17];&&
&&&&label.textColor&=&[UIColor&orangeColor];&&
&&&&label.shadowColor&=&[UIColor&lightGrayColor];&&
&&&&label.shadowOffset&=&CGSizeMake(1,0);&&
&&&&label.textAlignment&=&NSTextAlignmentC&&
&&&&label.lineBreakMode&=&NSLineBreakByTruncatingT&&
&&&&NSString&*string&=&label.&&
&&&&const&CGFloat&fontSize&=&16.0;&&
&&&&NSMutableAttributedString&*attrString&=&[[NSMutableAttributedString&alloc]&initWithString:string];&&
&&&&NSUInteger&length&=&[string&length];&&
&&&&UIFont&*baseFont&=&[UIFont&systemFontOfSize:fontSize];&&
&&&&[attrString&addAttribute:NSFontAttributeName&value:baseFont&range:NSMakeRange(0,&length)];&&
&&&&UIFont&*boldFont&=&[UIFont&boldSystemFontOfSize:fontSize];&&
&&&&[attrString&addAttribute:NSFontAttributeName&value:boldFont&range:[string&rangeOfString:@&Text&]];&&
&&&&UIFont&*italicFont&=&GetVariationOfFontWithTrait(baseFont,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&kCTFontTraitItalic);&&
&&&&[attrString&addAttribute:NSFontAttributeName&value:italicFont&&
&&&&&&&&&&&&&&&&&&&&&&&range:[string&rangeOfString:@&Label&]];&&
&&&&UIColor&*color&=&[UIColor&redColor];&&
&&&&[attrString&addAttribute:NSForegroundColorAttributeName&&
&&&&&&&&&&&&&&&&&&&&&&&value:color&&
&&&&&&&&&&&&&&&&&&&&&&&range:[string&rangeOfString:@&Content&]];&&
&&&&[attrString&addAttribute:NSBackgroundColorAttributeName&value:[UIColor&blueColor]&range:[string&rangeOfString:@&ent&]];&&
&&&&[attrString&addAttribute:NSFontAttributeName&value:[UIFont&fontWithName:@&Verdana-BoldItalic&&size:18]&range:[string&rangeOfString:@&Label&]];&&
&&&&label.numberOfLines&=&2;&&
&&&&NSMutableParagraphStyle&*&&
&&&&style&=&[[NSParagraphStyle&defaultParagraphStyle]&mutableCopy];&&
&&&&style.lineSpacing&=&10;&&
&&&&style.headIndent&=&10;&&
&&&&style.tailIndent&=&-10;&&
&&&&style.lineHeightMultiple&=&1.5;&&
&&&&style.alignment&=&NSTextAlignmentL&&
&&&&style.firstLineHeadIndent&=&20;&&
&&&&style.paragraphSpacing&=&10;&&
&&&&style.paragraphSpacingBefore&=&20;&&
&&&&[attrString&addAttribute:NSParagraphStyleAttributeName&value:style&range:NSMakeRange(0,&length)];&&
&&&&[attrString&addAttribute:NSKernAttributeName&value:@2&range:NSMakeRange(0,&length)];&&
&&&&[attrString&addAttribute:NSStrokeColorAttributeName&value:[UIColor&blueColor]&range:[string&rangeOfString:@&is&]];&&
&&&&[attrString&addAttribute:NSStrokeWidthAttributeName&value:@2&range:[string&rangeOfString:@&is&]];&&
&&&&[attrString&addAttribute:NSUnderlineStyleAttributeName&value:@(NSUnderlineStyleSingle)&range:[string&rangeOfString:@&text&]];&&
&&&&[attrString&addAttribute:NSUnderlineStyleAttributeName&value:@(NSUnderlineStyleThick)&range:[string&rangeOfString:@&label&]];&&
&&&&[attrString&addAttribute:NSStrikethroughStyleAttributeName&value:@(NSUnderlinePatternSolid&|&NSUnderlineStyleSingle)&range:[string&rangeOfString:@&things&]];&&
&&&&[attrString&addAttribute:NSStrikethroughColorAttributeName&value:[UIColor&blueColor]&range:[string&rangeOfString:@&things&]];&&
&&&label.attributedText&=&attrS&&
&&&&label.highlightedTextColor&=&[UIColor&redColor];&&
&&&&label.highlighted&=&NO;&&&
&&&&label.enabled&=&YES;&&
&&&&label.userInteractionEnabled&=&YES;&&
&&&&label.baselineAdjustment&=&UIBaselineAdjustmentN&&
UIFont&*&GetVariationOfFontWithTrait(UIFont&*baseFont,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&CTFontSymbolicTraits&trait)&{&&
&&&&CGFloat&fontSize&=&[baseFont&pointSize];&&
&&&&CFStringRef&&
&&&&baseFontName&=&(__bridge&CFStringRef)[baseFont&fontName];&&
&&&&CTFontRef&baseCTFont&=&CTFontCreateWithName(baseFontName,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fontSize,&NULL);&&
&&&&CTFontRef&ctFont&=&&
&&&&CTFontCreateCopyWithSymbolicTraits(baseCTFont,&0,&NULL,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&trait,&trait);&&
&&&&NSString&*variantFontName&=&&
&&&&CFBridgingRelease(CTFontCopyName(ctFont,&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&kCTFontPostScriptNameKey));&&
&&&&UIFont&*variantFont&=&[UIFont&fontWithName:variantFontName&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&size:fontSize];&&
&&&&CFRelease(ctFont);&&
&&&&CFRelease(baseCTFont);&&
&&&&return&variantF&&
UILable是iPhone界面最基本的控件,主要用来显示文本信息。
·常用属性和方法有:
CGRect rect = CGRectMake(100, 200, 50, 50);
UILabel *label = [[UILabel alloc] initWithFrame:rect];
2、text //设置和读取文本内容,默认为nil
label.text = @”文本信息”; //设置内容
NSLog(@”%@”, label.text); //读取内容
3、textColor //设置文字颜色,默认为黑色
lable.textColor = [UIColor redColor];
4、font //设置字体大小,默认17
label.font = [UIFont systemFontOfSize:20]; //?一般方法
label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法
label.font = [UIFont fontWithName:@&Arial& size:16]; //指定
字体的方法
//还有?一种从外部导入字体的方法。
5、textAlignment //设置标签文本对齐方式。
label.textAlignment = NSTextAlignmentC //还有
NSTextAlignmentLeft、 NSTextAlignmentRight.
6、numberOfLines //标签最多显示行数,如果为0则表示多行。
label.numberOfLines = 2;
7、enabled //只是决定了Label的绘制方式,将它设置
为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。
label.enable = NO;
8、highlighted //是否高亮显示
label.highlighted = YES;
label.highlightedTextColor = [UIColor orangeColor]; //高亮
显示时的文本颜色
9、ShadowColor //设置阴影颜色
[label setShadowColor:[UIColor blackColor]];
10、ShadowOffset //设置阴影偏移量
[label setShadowOffset:CGSizeMake(-1, -1)];
11、baselineAdjustment //如果adjustsFontSizeToFitWidth属性设
置为YES,这个属性就来控制文本基线的行为。
label.baselineAdjustment = UIBaselineAdjustmentN
UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters,
文本中线与label中线对齐。
UIBaselineAdjustmentNone, 文本最低端与label中线对齐。
12、Autoshrink //是否自动收缩
Fixed Font Size 默认,如果Label宽度小于文字长度时时,文字大小不自动缩放
minimumScaleFactor 设置最小收缩比例,如果Label宽度小于文字长度时,文字
进行收缩,收缩超过比例后,停止收缩。
minimumFontSize 设置最小收缩字号,如果Label宽度小于文字长度时,文字字号
减小,低于设定字号后,不再减小。//6.0以后不再使用了。
label.minimumScaleFactor = 0.5;
13、adjustsLetterSpacingToFitWidth //改变字母之间的间距来适应Label大小
myLabel.adjustsLetterSpacingToFitWidth = NO;
14、 lineBreakMode //设置文字过长时的显示格式
label.lineBreakMode = NSLineBreakByCharW以字符为显示单位显
示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByC剪切与文本宽度相同的内
容长度,后半部分被删除。
label.lineBreakMode = NSLineBreakByTruncatingH前面部分文字
以……方式省略,显示尾部文字内容。
label.lineBreakMode = NSLineBreakByTruncatingM中间的内容
以……方式省略,显示头尾的文字内容。
label.lineBreakMode = NSLineBreakByTruncatingT结尾部分的内容
以……方式省略,显示头的文字内容。
label.lineBreakMode = NSLineBreakByWordW以单词为显示单位显
示,后面部分省略不显示。
15、 adjustsFontSizeToFitWidth //设置字体大小适应label宽度
label.adjustsFontSizeToFitWidth = YES;
16、attributedText:设置标签属性文本。
NSString *text = @&first&;
NSMutableAttributedString *textLabelStr =
[[NSMutableAttributedString alloc]
initWithString:text];
[textLabelStr
setAttributes:@{NSForegroundColorAttributeName :
[UIColor lightGrayColor], NSFontAttributeName :
[UIFont systemFontOfSize:17]} range:NSMakeRange(11,
label.attributedText = textLabelS
17、竖排文字显示每个文字加一个换行符,这是最方便和简单的实现方式。
label.text = @&请\n竖\n直\n方\n向\n排\n列&;
label.numberOfLines = [label.text length];
18、计算UIlabel 随字体多行后的高度
CGRect bounds = CGRectMake(0, 0, 200, 300);
heightLabel = [myLabel textRectForBounds:bounds
limitedToNumberOfLines:20]; //计算20行后的Label的Frame
NSLog(@&%f&,heightLabel.size.height);
19、UILabel根据字数多少自动实现适应高度
UILabel *msgLabel = [[UILabel alloc]
initWithFrame:CGRectMake(15, 45, 0, 0)];
msgLabel.backgroundColor = [UIColor lightTextColor];
[msgLabel setNumberOfLines:0];
msgLabel.lineBreakMode = UILineBreakModeWordW
msgLabel.font = [UIFont fontWithName:@&Arial& size:12];
CGSize size = CGSizeMake(290, 1000);
msgLabel.text = @&获取到的deviceToken,我们可以通过webservice服务提
交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使
CGSize msgSie = [msgLabel.text sizeWithFont:fonts
constrainedToSize:size];
[msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];
20、渐变字体Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage
imageNamed:@&btn.png&]];
NSString *title = @&Setting&;
UILabel *titleLabel = [[UILabel alloc]
initWithFrame:CGRectMake(0, 0, 80, 44)];
titleLabel.textColor = titleC
titleLabel.text =
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];
21、Label添加边框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;
本文已收录于以下专栏:
相关文章推荐
[iOS]修改UILabel的行间距/段间距/缩进
- (void)viewDidLoad {
[super viewDidLoad];
UILabel继承自UIView是iOS中使用非常频繁的一个视图控件一般用于显示文字。
转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS
UI里的意义.
靠,这货其实就是间隔,起个名...
— Unmi 
iOS 的控件,只看到 UIButton 可以设置 Padding/Insets,即按钮上文字或图片与按钮边界的间隙,对与 CSS 来说叫做
iOS 的控件,只看到 UIButton 可以设置
Padding/Insets,即按钮上文字或图片与按钮边界的间隙,对与 CSS 来说叫做 Padding,在 iOS 中叫做 Insets,UI...
原文:点击打开链接
设置 UILabel 和 UITextField 的 Padding 或 Insets
iOS 的控件,只看到 UIB...
如题使用UILabel时候想要文字靠右显示,但是不想顶着UILabel的尾部显示,类似于设置UIButton 的edge。 UILabel好像不可以直接设置edge,所以选择使用UILabel的att...
UIKit框架-基础控件Swift版本: 3.UILabel方法\属性详解
UILabel 多行文字自动换行 (自动折行)
1.UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, ...
·UILable是iPhone界面最基本的控件,主要用来显示文本信息。
·常用属性和方法有:
CGRect rect = CGRectMake(100, 200, 50, 50);
他的最新文章
讲师:姜飞俊
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)博客分类:
1.text:设置标签显示文本。
2.attributedText:设置标签属性文本。
NSString *text = @"first";
NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:text];
[textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(11, 10)];
label.attributedText = textLabelS
3.font:设置标签文本字体。
label.font = [UIFont fontWithName:@"Arial" size:16];
4.textColor:设置标签文本颜色。
label.textColor = [UIColor blueColor];
5.textAlignment:设置标签文本对齐方式。
label.textAlignment = NSTextAlignmentC
6.lineBreakMode:设置标签文字过长时的显示方式。
label.lineBreakMode = NSLineBreakByCharW以字符为显示单位显示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByC剪切与文本宽度相同的内容长度,后半部分被删除。
label.lineBreakMode = NSLineBreakByTruncatingH前面部分文字以……方式省略,显示尾部文字内容。
label.lineBreakMode = NSLineBreakByTruncatingM中间的内容以……方式省略,显示头尾的文字内容。
label.lineBreakMode = NSLineBreakByTruncatingT结尾部分的内容以……方式省略,显示头的文字内容。
label.lineBreakMode = NSLineBreakByWordW以单词为显示单位显示,后面部分省略不显示。
7.enabled:设置文字内容是否可变。
8.adjustsFontSizeToFitWidth:文字内容自适应标签宽度。
9.adjustsLetterSpacingToFitWidth:根据字母的间隔自适应标签宽度,超出部分以……显示。
10.numberOfLines:标签最多显示行数。
11.minimumScaleFactor:设置最小字体,与minimumFontSize相同,minimumFontSize在IOS 6后不能使用。
12.highlightedTextColor:设置文本高亮显示颜色,与highlighted一起使用。
13.shadowColor:设置文本阴影颜色。
14.shadowColor:设置文本阴影与原文本的偏移量。label.shadowOffset = CGSizeMake(1.0, 5.0);
15.userInteractionEnabled:设置标签是否忽略或移除用户交互。默认为NO。
16.preferredMaxLayoutWidth:优先选择标签布局的最大宽度。
17.baselineAdjustment:如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
label4.baselineAdjustment = UIBaselineAdjustmentN
UIBaselineAdjustmentAlignBaselines=0,默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters,
文本中线与label中线对齐。
UIBaselineAdjustmentNone,
文本最低端与label中线对齐。
浏览: 4529 次
来自: 北京
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'我有一个带两行文本的UILabel 。 有时,当文本太短时,此文本显示在标签的垂直中心。
如何垂直对齐文本,使它的始终位于 UILabel的顶部?
15年06月12日
共30个回答
无法在 UILabel 上设置垂直对齐方式,但通过更改标签的框架可以获得相同的效果。 我把我的标签做成橙色,这样你可以清楚地看到发生了什么。
下面是快速和简单的方法:
[myLabel sizeToFit];
如果有一个标签较长的标签,该标签会使多行上,请将 numberOfLines 设置为 0 ( 零表示无限的行数) 。
myLabel.numberOfLines = 0;
[myLabel sizeToFit];
我将把我的标签放在代码中,这样你就可以看到发生了什么。 你可以在接口生成器中设置大部分内容。 我的设置是一个基于视图的应用,我在Photoshop中使用了背景图像来显示页边距( 20磅) 。 标签是橙色的颜色,所以你可以看到它的尺寸。
- (void)viewDidLoad
[super viewDidLoad];
//20 point top and left margin. Sized to leave 20 pt at right.
CGRect labelFrame = CGRectMake(20, 20, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setBackgroundColor:[UIColor orangeColor]];
NSString *labelText = @"I am the very model of a modern Major-General, I've information vegetable, animal, and mineral";
[myLabel setText:labelText];
//Tell the label to use an unlimited number of lines
[myLabel setNumberOfLines:0];
[myLabel sizeToFit];
[self.view addSubview:myLabel];
使用 sizeToFit的一些限制在center-或者right-aligned文本中发挥作用。 以下是发生的情况:
//myLabel.textAlignment = NSTextAlignmentR
myLabel.textAlignment = NSTextAlignmentC
[myLabel setNumberOfLines:0];
[myLabel sizeToFit];
标签仍然使用固定的top-left角点调整大小。 你可以在变量中保存标签的原始宽度,并将它的设置为 sizeToFit,或者给它一个固定的宽度来解决这些问题:
myLabel.textAlignment = NSTextAlignmentC
[myLabel setNumberOfLines:0];
[myLabel sizeToFit];
CGRect myFrame = myLabel.
//Resize the frame's width to 280 (320 - margins)
//width could also be myOriginalLabelFrame.size.width
myFrame = CGRectMake(myFrame.origin.x, myFrame.origin.y, 280, myFrame.size.height);
myLabel.frame = myF
注意 sizeToFit 将尊重你最初的最小标签宽度。 如果你上来就用 100标签 sizeToFit 100宽可以调用就可以了,它会给你返回一个( 可能很高) 标签( 或者稍微少一点) 宽度。 调整大小之前,你可能希望将标签设置为所需的最小宽度。
需要注意的其他事项:
是否尊重 lineBreakMode 取决于它的设置方式。 NSLineBreakByTruncatingTail ( 默认值) 在 sizeToFit 之后被忽略,另外两个截断模式( 头部和中部) 。 NSLineBreakByClipping 也被忽略。NSLineBreakByCharWrapping 正常工作。 框架宽度仍然缩小到最右边的字母。
为NIBs和 Storyboard 在注释中使用自动布局提供了一个修复:
如果你的标签是包含在一个笔尖或者 Storyboard 作为subview的view 一个 ViewController,用的是自动布局,你接着在上述 sizeToFit 调入 viewDidLoad subviews viewDidLoad 被调用后并不可行,因为自动布局大小和位置将立即消除-的效果你 sizeToFit 电话。 在 viewDidLayoutSubviews 内将 work,但是,调用
我原来回答( 对于后代/参考):
使用 NSString 方法 sizeWithFont:constrainedToSize:lineBreakMode: 要计算拟合字符串所需的框架高度,请设置。
使用要插入的文字调整标签的框架大小。 这样你可以适应任意数量的线条。
CGSize maximumSize = CGSizeMake(300, 9999);
NSString *dateString = @"The date today is January 1st, 1999";
UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize dateStringSize = [dateString sizeWithFont:dateFont
constrainedToSize:maximumSize
lineBreakMode:self.dateLabel.lineBreakMode];
CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);
self.dateLabel.frame = dateF
这里页面具有相同解决方案的一些不同代码:
1 ) 设置新文本:
myLabel.text = @"Some Text"
2 ) 将行的maximum number 设置为 0 ( 自动):
myLabel.numberOfLines = 0
3 ) 将标签的框架设置为最大大小:
myLabel.frame = CGRectMake(20,20,200,800)
4 ) 调用 sizeToFit 以缩小帧大小,使内容刚好适合:
[myLabel sizeToFit]
标签框架的高度和宽度足以适应你的文字。 左上角应该不变。 我只用左对齐的文本测试了这个。 对于其他对齐方式,你可能需要在以后修改框架。
同时,我的标签启用了自动换行功能。
引用扩展解决方案:
for(int i=1; i &newLinesToP i++)
self.text = [self.text stringByAppendingString:@"n"];
应该被替换为
for(int i=0; i&newLinesToP i++)
self.text = [self.text stringByAppendingString:@"n"];
在每个添加的换行符中需要额外的空间,因为 UILabels'尾部的回车似乎被忽略:(
类似地,alignBottom也应该更新,使用 @" n@%" 代替 "n@%" ( 循环初始化必须替换为"进行( int i=0.。"。
以下扩展适用于我:
//-- file: UILabel+VerticalAlign.h
#pragma mark VerticalAlign
@interface UILabel (VerticalAlign)
- (void)alignT
- (void)alignB
//-- file: UILabel+VerticalAlign.m
@implementation UILabel (VerticalAlign)
- (void)alignTop {
CGSize fontSize = [self.text sizeWithFont:self.font];
double finalHeight = fontSize.height * self.numberOfL
double finalWidth = self.frame.size.//expected width of label
CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height)/fontSize.
for(int i=0; i&newLinesToP i++)
self.text = [self.text stringByAppendingString:@"n"];
- (void)alignBottom {
CGSize fontSize = [self.text sizeWithFont:self.font];
double finalHeight = fontSize.height * self.numberOfL
double finalWidth = self.frame.size.//expected width of label
CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height)/fontSize.
for(int i=0; i&newLinesToP i++)
self.text = [NSString stringWithFormat:@" n%@",self.text];
然后每个 assignment, yourLabel文本后调用 [yourLabel alignTop]; 或者
就像上面的答案,但它不是很正确,或者很容易进入代码,所以我把它清理干净一点。 将这里扩展添加到它自己的. h 和. m 文件中,或者直接粘贴到要使用的实现之上:
#pragma mark VerticalAlign
@interface UILabel (VerticalAlign)
- (void)alignT
- (void)alignB
@implementation UILabel (VerticalAlign)
- (void)alignTop
CGSize fontSize = [self.text sizeWithFont:self.font];
double finalHeight = fontSize.height * self.numberOfL
double finalWidth = self.frame.size.//expected width of label
CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height)/fontSize.
for(int i=0; i&= newLinesToP i++)
self.text = [self.text stringByAppendingString:@" n"];
- (void)alignBottom
CGSize fontSize = [self.text sizeWithFont:self.font];
double finalHeight = fontSize.height * self.numberOfL
double finalWidth = self.frame.size.//expected width of label
CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height)/fontSize.
for(int i=0; i &newLinesToP i++)
self.text = [NSString stringWithFormat:@" n%@",self.text];
然后使用,将文本放入标签,然后调用适当的方法对齐它:
[myLabel alignTop];
[myLabel alignBottom];
更快的( 和 dirtier ) 方法是将uilabel中断模式的行设置为"剪辑"并添加一个固定的换行符。
myLabel.lineBreakMode = UILineBreakModeC
myLabel.text = [displayString stringByAppendingString:"nnnn"];
这个解决方案对于每个--都不起作用,如果你仍然想在字符串末尾显示"。。",那么你需要使用一个较长的代码 Fragment,这将会让你得到需要的代码。
在使用 UILabel 而不愿使用以防万一它是任何帮助给所有人,我有同样的问题,但能够解决所面对的问题简单地通过 switching. 我欣赏这不是针对所有人,因为功能有点不同。
如果要使用 switch 来使用 UITextView,可以关闭所有滚动视图属性以及启用用户交互。。 这将迫使它更像一个标签。
而不是 UILabel,你可以使用具有垂直对齐选项的UITextField:
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentC
textField.userInteractionEnabled = NO;//Don't allow interaction
我已经很长时间了,我想分享我的解决方案。
这将给你一个 UILabel,它将autoshrink文本缩小到 0.5个刻度并垂直居中文本。 这些选项在 Storyboard/ib中也可用。
[labelObject setMinimumScaleFactor:0.5];
[labelObject setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];
在接口生成器中
将 UILabel 设置为最大可能文本的大小
在属性检查器中将 Lines 设置为'0'
在你的代码中
设置标签的文本
在你的标签上调用 sizeToFit
self.myLabel.text = @"Short Title";
[self.myLabel sizeToFit];
LabelTopAlign
#import &UIKit/UIKit.h&
@interface KwLabelTopAlign : UILabel {
#import"KwLabelTopAlign.h"
@implementation KwLabelTopAlign
- (void)drawTextInRect:(CGRect)rect {
int lineHeight = [@"IglL" sizeWithFont:self.font constrainedToSize:CGSizeMake(rect.size.width, 9999.0f)].
if(rect.size.height& = lineHeight) {
int textHeight = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(rect.size.width, rect.size.height)].
int yMax = textH
if (self.numberOfLines& 0) {
yMax = MIN(lineHeight*self.numberOfLines, yMax);
[super drawTextInRect:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, yMax)];
下面是一个实现相同的简单实现:
#import"KwLabelTopAlign.h"
@implementation KwLabelTopAlign
- (void)drawTextInRect:(CGRect)rect
CGFloat height = [self.text sizeWithFont:self.font
constrainedToSize:rect.size
lineBreakMode:self.lineBreakMode].
if (self.numberOfLines!= 0) {
height = MIN(height, self.font.lineHeight * self.numberOfLines);
rect.size.height = MIN(rect.size.height, height);
[super drawTextInRect:rect];
Copyright (C) 2011 HelpLib All rights reserved. &&

我要回帖

更多关于 minimum 如何输入 的文章

 

随机推荐