繼承關係:UIView : UIResponder : NSObject佈局
///UILabel 顯示的文本只讀,沒法編輯,能夠根據文字個數自動換行;字體
///UITextField 可編輯本文,可是沒法換行,只能一行顯示;當點擊鍵盤上的return時,會觸發事件。ui
////UITextView 可編輯文本,提供換行功能。spa
//建立uilabel UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
//設置背景色 label1.backgroundColor = [UIColor grayColor]; //是否能與用戶交互 label1.userInteractionEnabled = YES; //設置tag label1.tag = 91;
//設置標籤文本 label1.text = @"Hello world!"; //設置標籤文本字體和字體大小 label1.font = [UIFont fontWithName:@"Arial" size:30]; //字體、字號 //系統字體 label.font = [UIFont systemFontOfSize:30.0]; //加粗 label.font = [UIFont boldSystemFontOfSize:30.0]; //斜體 label.font = [UIFont italicSystemFontOfSize:30.0]; //拿到全部字體 NSArray* fonts = [UIFont familyNames]; //經過字體名字設置字體 label.font = [UIFont fontWithName:[fonts objectAtIndex:5] size:30.0]; //文本最多行數,爲0時沒有最大行數限制 自動換行 label1.numberOfLines = 2; //最小字體,行數爲1時有效,默認爲0.0 label1.minimumFontSize = 10.0; //段 行 字間距 label.numberOfLines = 0; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSKernAttributeName : @(1.5f)}];//字間距 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:5.0];//設置行間距 [paragraphStyle setParagraphSpacing:50.0];//設置段間距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])]; label.attributedText = attributedString;
//設置文本對其方式 label1.textAlignment = UITextAlignmentCenter; //文本對齊方式有如下三種 //typedef enum { // UITextAlignmentLeft = 0,左對齊 // UITextAlignmentCenter,居中對齊 // UITextAlignmentRight, 右對齊 //} UITextAlignment; //注:如今的版本已經棄用上述對齊方式,如下方爲主 /* Values for NSTextAlignment */ typedef NS_ENUM(NSInteger, NSTextAlignment) { NSTextAlignmentLeft = 0, // Visually left aligned #if TARGET_OS_IPHONE NSTextAlignmentCenter = 1, // Visually centered NSTextAlignmentRight = 2, // Visually right aligned #else /* !TARGET_OS_IPHONE */ NSTextAlignmentRight = 1, // Visually right aligned NSTextAlignmentCenter = 2, // Visually centered #endif NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned. NSTextAlignmentNatural = 4, // Indicates the default alignment for script } NS_ENUM_AVAILABLE_IOS(6_0);
//文本顏色 label1.textColor = [UIColor blueColor]; //顏色漸變 label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]]; //文本高亮 label1.highlighted = YES; //高亮顏色 label1.highlightedTextColor = [UIColor blueColor]; //文本是否可變 :未激活,灰色,設置顏色無效 label1.enabled = YES; //去掉label背景色 label1.backgroundColor = [UIColor clearColor]; //文本陰影顏色 label1.shadowColor = [UIColor grayColor]; //陰影偏移量 label1.shadowOffset = CGSizeMake(1.0, 1.0);
//超出label邊界文字的截取方式 label1.lineBreakMode = UILineBreakModeTailTruncation; //截取方式有如下6種 //typedef enum { // UILineBreakModeWordWrap = 0, 以空格爲邊界,保留整個單詞 // UILineBreakModeCharacterWrap, 保留整個字符 // UILineBreakModeClip, 到邊界爲止 // UILineBreakModeHeadTruncation, 省略開始,以……代替 // UILineBreakModeTailTruncation, 省略結尾,以……代替 // UILineBreakModeMiddleTruncation,省略中間,以……代替,多行時做用於最後一行 //} UILineBreakMode;
//調整基線 需設置文本文字自適應大小 label1.adjustsFontSizeToFitWidth = YES; //baselineAdjustment這個值控制文本的基線位置,只有文本行數爲1是有效 label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters; //有三種方式 //typedef enum { // UIBaselineAdjustmentAlignBaselines = 0, 默認值文本最上端於label中線對齊 // UIBaselineAdjustmentAlignCenters,//文本中線於label中線對齊 // UIBaselineAdjustmentNone,//文本最低端與label中線對齊 //} UIBaselineAdjustment;
/* - (void)buttonClick:(UIButton*)button{ //父視圖經過tag值獲取子視圖的指針對象 /* 子視圖能夠設置一個tag值,而後添加到父視圖上,父視圖就能夠經過這個tag值拿到子視圖的指針。 tag值也能夠保存一些用戶的信息。 */ UILabel* label = (UILabel*)[self.view viewWithTag:100]; label.text = @"我被修改了"; } */
//自動適應高度 CGSize size = CGSizeMake(280, 1000); UIFont *lableFont = [UIFont fontWithName:@"Arial" size:12.0]; CGSize labelSize = [label.text sizeWithFont:lableFont constrainedToSize:size];//7.0 ,現以下 //[label1.text boundingRectWithSize:(CGSize) options:(NSStringDrawingOptions) attributes:(NSDictionary *) context:(NSStringDrawingContext *)] label.frame = CGRectMake(0, 0, 280, labelSize.height);
//簡單富文本 NSString *text = @"我是大壞蛋哦!"; NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text]; [attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor], NSFontAttributeName : [UIFont systemFontOfSize:18]} range:NSMakeRange(2, 3)]; label1.attributedText = attributeString; //修改Range 所包含文本的 顏色、字體大小
//圓角 label.layer.backgroundColor =[UIColor yellowColor].CGColor; label.layer.cornerRadius =5.0; label.layer.frame = CGRectInset(label.layer.frame, 20, 20); ///邊框 label.layer.borderColor = [[UIColor redColor] CGColor]; label.layer.borderWidth = 2;
[self.view addSubview:label1]; [label1 release];