UI基础之--UILabel
来源:互联网 发布:超星泛雅网络课程 编辑:程序博客网 时间:2024/05/22 18:26
self.label.text = @"whiteColor";
self.label.font = [UIFont systemFontOfSize:20];
/** 设置高亮文本颜色,与highlighted属性一起使用,只有设置highlighted为YES才生效 */
self.label.highlightedTextColor = [UIColor redColor];
/** 设置行数,0为多行 */
self.label.numberOfLines = 1;
/** 默认为NO,一般添加手势才会设置为YES */
self.label.userInteractionEnabled = YES;
/** 默认为YES,若设置为NO,文字颜色会变暗,而且高亮属性highlighted失效 */
self.label.enabled = YES;
/** 设置阴影 */
self.label.shadowColor = [UIColor orangeColor];
self.label.shadowOffset = CGSizeMake(1, 2);
/** 文本对齐方式
NSTextAlignmentLeft 默认左对齐
NSTextAlignmentCenter 居中
NSTextAlignmentRight 右对齐
*/
self.label.textAlignment = NSTextAlignmentLeft;
/** 可根据字数来调整字体大小,值是0~1,与adjustsFontSizeToFitWidth属性一起使用和行数为1的时候才有效 */
self.label.minimumScaleFactor = 8.0/[UIFont labelFontSize];
/** 在固定的宽高度下,自动调整字体大小,以全部显示字体 */
self.label.adjustsFontSizeToFitWidth = YES;
/** 如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与label中线对齐
UIBaselineAdjustmentAlignCenters,文本中线与label中线对齐
UIBaselineAdjustmentNone,文本最低端与label中线对齐
*/
self.label.baselineAdjustment = UIBaselineAdjustmentNone;
/* 设置标签文字过长时的显示方式
NSLineBreakByWordWrapping = 0, 以单词为单位显示,超出部分省略不显示 (adjustsFontSizeToFitWidth属性失效)
NSLineBreakByCharWrapping,以字符为显示单位显示,超出部分省略不显示 (adjustsFontSizeToFitWidth属性失效)
NSLineBreakByClipping,剪切与文本宽度相同的内容长度,超出部分不显示
NSLineBreakByTruncatingHead,Truncate at head of line: "...wxyz"
NSLineBreakByTruncatingTail,Truncate at tail of line: "abcd..."
NSLineBreakByTruncatingMiddleTruncate middle of line: "ab...yz"
*/
self.label.lineBreakMode = NSLineBreakByClipping;
/** 基本不用的属性 */
self.label.minimumFontSize = 50;
self.label.allowsDefaultTighteningForTruncation = YES;
self.label.adjustsLetterSpacingToFitWidth = YES;
self.label.preferredMaxLayoutWidth = self.view.frame.size.width;
// 点击按钮后设置highlighted和attributedText两个属性
- (IBAction)action:(UIButton *)sender {
self.label.highlighted = !self.label.highlighted;
/** 设置文本样式
NSFontAttributeName 字体默认是12号
NSParagraphStyleAttributeName 段落样式
NSForegroundColorAttributeName 指定字体颜色
NSBackgroundColorAttributeName 字体背景颜色
NSLigatureAttributeName 连体字符
NSKernAttributeName 字距的像素
NSStrikethroughStyleAttributeName指定字上加删除线
NSUnderlineStyleAttributeName 指定字加下划线
NSStrokeColorAttributeName 填充部分颜色
NSStrokeWidthAttributeName 填充宽度
NSShadowAttributeName 阴影
NSUnderlineColorAttributeName 下划线颜色
*/
NSMutableAttributedString *attributed = [[NSMutableAttributedStringalloc]initWithString:self.label.text];
// 设置第一个字符的颜色为黑色
[attributed addAttribute:NSForegroundColorAttributeNamevalue:[UIColorblackColor]range:NSMakeRange(0,1)];
self.label.attributedText = attributed;
}
/**
* 根据文字自动设置size
*/
- (CGSize)getCurrentTextSize {
CGSize size =CGSizeMake(MAXFLOAT,MAXFLOAT);
if (self.label.text.length > 0) {
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >=70000
size =[self.label.textboundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.label.font}
context:nil].size;
#else
size = [self.text sizeWithFont:self.font
constrainedToSize:size
lineBreakMode:NSLineBreakByCharWrapping];
#endif
}
return size;
}
PS:希望能帮助到需要的人,如有不对,欢迎指点!
- UI基础之--UILabel
- UI基础之UIview,UILabel,UIWindow
- UI基础学习之(二):UILabel
- iOS UI基础控件之UILabel
- UI基础:UILabel.UIFont
- IOS#UI之UIlabel
- UI控件之UILabel
- Swift UI学习之基础:UIButton,UILabel and UISlider
- Swift UI学习之基础:UIButton,UILabel and UISlider
- Swift UI学习之基础:UIButton,UILabel and UISlider
- iOS UI界面之UILabel
- iOS之UI学习-UILabel
- UI之UILabel和UIImageView
- UI 基础控件(一)-----------UILabel
- UI基础--UILabel的相关属性设置
- iOS-UI-基础控件核心API-UILabel
- UI基础2控件:UILabel,UIImageView
- iOS基础——UI控件之UIView、UITextField、UILabel、UIColor、UIButton、UIImageView
- ZooKeeper实战应用之【统一配置管理】
- Android——自定义View loading效果
- 中国“互联网+”案例100征集活动正式启动
- android开发轻量级数据库SQLite
- LeetCode Implement strStr(kmp或者BM)
- UI基础之--UILabel
- solr
- Cross-Orign Resource Sharing简介
- 关于Android 6.0 调用 要求使用权限代码报红色问题
- Delphi中挂接IWebBrowser2的自动化事件
- JAVA IO
- 解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
- 图片像素与色彩模式
- ```C#学习(五)···