UILabel - 简单整理

虽然很基本简单,但还是有那么几个比较少用的属性,今天在用cell时,竟然关系到Label 的属性,在此整理下。

常用基本属性

  • 最常用
    label.text = @"wertyu;";
    label.font = [UIFont systemFontOfSize:20];
    label.textColor = [UIColor redColor];
    label.textAlignment = NSTextAlignmentCenter;
  • 格式调整
    label.numberOfLines = 1;
    label.lineBreakMode = NSLineBreakByCharWrapping;
    /*
     typedef NS_ENUM(NSInteger, NSLineBreakMode) {
     NSLineBreakByWordWrapping = 0,      // Wrap at word boundaries, default
     NSLineBreakByCharWrapping,  // Wrap at character boundaries
     NSLineBreakByClipping,  // Simply clip
     NSLineBreakByTruncatingHead, // Truncate at head of line: "...wxyz"
     NSLineBreakByTruncatingTail, // Truncate at tail of line: "abcd..."
     NSLineBreakByTruncatingMiddle // Truncate middle of line:  "ab...yz"
     };
     */
    label.adjustsFontSizeToFitWidth = NO;// 根据宽度调整字体大小
    label.minimumScaleFactor = 0.2;// 代替了原来的最小字体大小,新的是比例
    label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;// 正常的baseLine(可以调整)
    /*
     typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {
     UIBaselineAdjustmentAlignBaselines = 0,
     UIBaselineAdjustmentAlignCenters,
     UIBaselineAdjustmentNone,
     };
     */
  • 加特效
    // 属性字符串(具体的看 NSAttributedString 类)
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],
                                 NSForegroundColorAttributeName:[UIColor redColor]};
    label.attributedText = [[NSAttributedString alloc] initWithString:@"3456789" attributes:attributes];
    
    // 阴影
    label.shadowColor = [UIColor greenColor];
    label.shadowOffset = CGSizeMake(2, 2);
    
  • 高亮
    label.highlightedTextColor = [UIColor greenColor];
    label.highlighted = YES;// 这个真是坑爹啊,在cell点击效果中添加label 高亮颜色,默认就设为YES了,改一下上面的highlightedTextColor 就可以了。
  • 可用性
    label.userInteractionEnabled = YES;// 是否可以交互,默认NO,开启可添加手势
    label.enabled = NO;// 只感觉有变灰效果。。默认YES,可用
  • 重绘
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;// 调整rect,比如第一行缩进多少的语文课代表形式。
- (void)drawTextInRect:(CGRect)rect;// 画text
  • 自动布局相关
preferredMaxLayoutWidth = 100;// ios6,需要设置,label的宽是多少,然后才能自动换行计算高度,现在不需要了。

其他

@property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0); // default is NO
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,693评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,099评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,632评论 4 61
  • 小五人是不错的,也挺聪明的,就像他投篮技术很好的那样,同时也是有个性有想法的人,就像他的偏执,对爱情的不执着那样!...
    睡过坟堆的人阅读 1,756评论 0 0
  • 于晓威 我的读书习惯,大概是小时候,由家庭和学校共同培养的。 因为...
    于晓威阅读 3,610评论 0 6

友情链接更多精彩内容