关于Label⽂字样式的⼀些设置
- (void)loadView
{
[super loadView];
//1.UILable的⼤⼩⾃适应实例:
// ***** label基本属性 *****
UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 20, 2, 2)]; // 设定位置与⼤⼩
// [myLabel tFont:[UIFont fontWithName:@"Helvetica" size:20.0]]; // 字体和⼤⼩
// myLabel.font = [UIFont boldSystemFontOfSize:18]; // ⿊体18号字
[myLabel tNumberOfLines:0]; // ⾏数,只有设为 0 才可以⾃适应
[myLabel tBackgroundColor:[UIColor clearColor]]; // 背景⾊
myLabel.shadowColor = [UIColor darkGrayColor]; // 阴影颜⾊
myLabel.shadowOfft = CGSizeMake(1.0,1.0); // 阴影偏移量
NSString *text = @"abcdefghigklmnopqrstuvwxyz";
UIFont *font = [UIFont fontWithName:@"Helvetica" size:20.0];
// ******* 根据要显⽰的text计算label⾼度 *******
// iOS 7 之后被弃⽤
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
lineBreakMode:UILineBreakModeWordWrap];
// iOS 7 后 :
{
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
size =[text boundingRectWithSize:size options:NSStringDrawingUsLineFragmentOrigin |NSStringDrawingUsFontLeading attributes:tdic context:nil].size;
}
CGRect rect = myLabel.frame;
rect.size = size;
[myLabel tFrame:rect];
[myLabel tText:text];
myLabel.shadowColor = [UIColor darkGrayColor];//阴影颜⾊
myLabel.shadowOfft = CGSizeMake(2.0,2.0);//阴影⼤⼩
[lf.view addSubview:myLabel];
//2.UILable的基本⽤法
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 30.0)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 440.0, 200.0, 50.0)];
//设置显⽰⽂字
< = @"label1";
< = @"label2";
< = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--11个"; = @"label4--label4--label4--label4--4个~~~";
< = @"label5--label5--label5--label5--label5--label5--6个";
< = @"label6";
< = @"label7";
//设置字体: ⿊体字,正常的是 SystemFontOfSize
label1.font = [UIFont boldSystemFontOfSize:20];
//设置⽂字颜⾊
//设置背景颜⾊
label1.backgroundColor = [UIColor clearColor];
label2.backgroundColor = [UIColor colorWithRed:0.5f green:30/255.0f blue:0.3f alpha:0.5f];
//设置⽂字位置
//设置字体⼤⼩适应label宽度
label4.adjustsFontSizeToFitWidth = YES; // 字越多,越窄
//设置label的⾏数
label5.numberOfLines = 2;
//设置⾼亮
label6.highlighted = YES;
label6.highlightedTextColor = [UIColor orangeColor];
//设置阴影
label7.shadowColor = [UIColor redColor];
label7.shadowOfft = CGSizeMake(1.0,1.0);
//设置是否能与⽤户进⾏交互
label7.urInteractionEnabled = YES;
//设置label中的⽂字是否可变,默认值是YES
//设置⽂字过长时的显⽰格式
// iOS 6 后弃⽤ UILineBreakModeMiddleTruncation
{
label3.lineBreakMode = UILineBreakModeMiddleTruncation; //截去中间// typedef enum {
// UILineBreakModeWordWrap = 0, //以空格为边界,保留单词。
// UILineBreakModeCharacterWrap,
// UILineBreakModeClip, //截去多余部分
/
/ UILineBreakModeHeadTruncation, //截去头部
// UILineBreakModeTailTruncation, //截去尾部
// UILineBreakModeMiddleTruncation, //截去中间
// } UILineBreakMode;
}
label3.lineBreakMode = NSLineBreakByWordWrapping;
// typedef NS_ENUM(NSInteger, NSLineBreakMode) {
// NSLineBreakByWordWrapping = 0, // //以空格为边界,保留单词。
// NSLineBreakByCharWrapping, // 截取到范围内(字符串)
// NSLineBreakByClipping, //简单剪裁,到边界为⽌
// NSLineBreakByTruncatingHead, // 从前⾯开始裁剪字符串: "...wxyz"
/
/ NSLineBreakByTruncatingTail, // 从后⾯开始裁剪字符串: ""
// NSLineBreakByTruncatingMiddle // 从中间裁剪字符串: "ab...yz"
// }
// 我们使⽤的xcode是5.0版本,默认使⽤的是sdk7.0。使⽤sdk7.0会导致两者效果完全相同。使⽤sdk6.1运⾏的时候可以有效区分开// 如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制⽂本基线的⾏为
label4.balineAdjustment = UIBalineAdjustmentAlignCenters;
// typedef enum {
// UIBalineAdjustmentAlignBalines,
// UIBalineAdjustmentAlignCenters,
// UIBalineAdjustmentNone,
// } UIBalineAdjustment;
// ***** 根据属性给固定位置的字体更换颜⾊或字体⼤⼩ *****
NSString *originStr = @"Hello,World!";
//创建 NSMutableAttributedString
NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];
//添加属性
//给所有字符设置字体为Zapfino,字体⾼度为15像素
[attributedStr01 addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 15] range: NSMakeRange(0, originStr.length)];
/
/分段控制,最开始4个字符颜⾊设置成蓝⾊
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 5)];
//分段控制,第5个字符开始的3个字符,即第5、6、7字符设置为红⾊
[attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(5, 3)];
//赋值给显⽰控件label01的 attributedText
UILabel *label01 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 520.0, 200.0, 50.0)];
label01.attributedText = attributedStr01;
// ***** 给 固定 ⽂字替换成 指定 符号 (多⽤于禁⽤字) *****
NSString *arch = @"王⽊⽊";
NSString *replace = @"***";
NSMutableString *mstr = [[NSMutableString alloc]initWithString:@"dafkasdhf王⽊⽊kasddsf"];
NSRange range = [mstr rangeOfString:arch];
[mstr replaceCharactersInRange:range withString:replace];
UILabel *labelStr = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 580.0, 200.0, 50.0)];