iOS04- UIButton的小常识

有些时候我们想让UIButton的title居左对齐,我们设置

btn.textLabel.textAlignment = UITextAlignmentLeft

是没有作用的,我们需要设置

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是问题又出来,此时文字会紧贴到做边框,我们可以设置

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距离做边框保持10个像素的距离。

=======================================================

设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

[btn.titleLabel setTextColor:[UIColorblackColor]];

btn.titleLabel.textColor=[UIColor redColor];

而是用:

[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

  • //设置字体大小  
  •     label1.font = [UIFont systemFontOfSize:20.0];  
  •     label2.font = [UIFont boldSystemFontOfSize:20.0];//粗体
  •    //linebreakmode 文字打断模式(过长) 默认位NSLineBreakByWordWrapping  
  •     label2.lineBreakMode = NSLineBreakByClipping;
  •   //Managing Highlight values  
  •     //设置高亮  
  •     label1.highlighted = YES;  
  •     label1.highlightedTextColor = [UIColor orangeColor]; 

     添加边框,圆角和弧度。

avatarImage = [UIButtonbuttonWithType:UIButtonTypeCustom];

        //给按钮加一个白色的板框

        avatarImage.layer.borderColor = [[UIColorwhiteColorCGColor];

        avatarImage.layer.borderWidth =1.0f;

        //给按钮设置弧度,这里将按钮变成了圆形

        avatarImage.layer.cornerRadius =25.0f;

        avatarImage.backgroundColor = [UIColorredColor];

        avatarImage.layer.masksToBounds =YES;

  前者用来设置文字的大小,后者后来设置文字的粗细

    UIFont *font = [UIFont systemFontOfSize:17.0f weight:UIFontWeightUltraLight];  

    self.lable.font = font;  

 

[objc]  view plaincopy
  1. NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:14],  
  2.                                  NSForegroundColorAttributeName: [UIColor redColor],  
  3.                                  NSStrokeWidthAttributeName: @-10};  
[objc]  view plaincopy
  1. self.lable.attributedText = [[NSAttributedString alloc] initWithString:@"字符串" attributes:attributes];  

通过将字典数据attributes将值赋给lable.attributeText,可以设置lable中text的字号大小,颜色,外边宽度,外边颜色,甚至是阴影等各种属性。
[objc]  view plaincopy
  1. NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] init];  
  2. [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];  
  3. self.lable.attributedText = attributeStr;  


通过创建可变的NSMutableAttributeString,然后调用添加方法,不仅可以设置lable的各种属性,还可以设置每种属性的影响范围,更加灵活。

最后再补上各种常用属性的功能:

NSFontAttributeName 设置字号

NSParagraphStyleAttributeName 设置段落样式

NSForegroundColorAttributeName 设置文字颜色

NSBackgroundColorAttributeName 设置背景颜色

NSStrokeColorAttributeName 设置外边颜色

NSStrokeWidthAttributeName 设置外边宽度

NSUnderlineStyleAttributeName 设置下划线

NSUnderlineColorAttributeName 设置划线颜色

NSStrikethroughStyleAttributeName 设置划线

NSShadowAttributeName 设置阴影

NSVerticalGlyphFormAttributeName 设置文本布局为横(输入值0即可,iOS不支持竖排版)

NSObliquenessAttributeName 设置字体倾斜

NSExpansionAttributeName 设置文字扁平化

PS1:外边宽度和外边颜色同事设置时,文字会变为空心属性。但是当外边宽度为负值时,不会空心。

PS2:设置阴影单独不可用,要和NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName一起使用才行。

继续添加iOS支持的中文字体的模样

Heiti SC

STHeitiSC-Light

STHeitiSC-Medium
Heiti TC
STHeitiTC-Light
STHeitiTC-Medium
Hiragino Kaku Gothic ProN
HiraKakuProN-W3
HiraKakuProN-W6
Hiragino Mincho ProN
HiraMinProN-W3
HiraMinProN-W6

 

 

转载于:https://www.cnblogs.com/ytmaylover/p/5050202.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值