iOS应用UI开发中的字体和按钮控件使用指南

2020-01-14 19:25:05王振洲
易采站长站为您分析iOS应用UI开发中的字体和按钮控件使用指南,分别简单讲解了UILabel和UIButton的用法,需要的朋友可以参考下  

UILabel的使用
一、初始化

复制代码
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];
     
[self.view addSubview:myLabel];
二、设置文字

 

 ①、设置默认文本

复制代码
NSString *text = @"标签文本";
myLabel.text = text;
效果:

 

iOS应用UI开发中的字体和按钮控件使用指南

②、设置标签文本(此属性是iOS6.0之后才出现,如若不是必要,不建议使用此属性)

复制代码
NSString *text = @"其实没什么";
     
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
     
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
     
myLabel.attributedText = attributeString;
效果:

 

iOS应用UI开发中的字体和按钮控件使用指南

关键字标红的效果

复制代码
NSString *keyword = @"脚本";
NSString *result = @"ASPKU";
 
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:result];
 
// 获取标红的位置和长度
NSRange range = [result rangeOfString:keyword];
 
// 设置标签文字的属性
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];