美文网首页iOS Developer
Swift—UILabel的常见用法

Swift—UILabel的常见用法

作者: 见哥哥长高了 | 来源:发表于2017-03-14 13:14 被阅读201次

    声明:以下内容只是用来作为个人学习、熟悉swift语言开发或者今后查阅资料使用,也会随着语言的不不断更新而不断的进行更新发布....

    UILabel用来在界面上显示一行或多行文本,下面就介绍常见属性与基本用法。

    1、UILabel创建####

    //创建的时候设置frame
    let testLable=UILabel(frame:CGRect(x:20,y:100,width:self.view.frame.size.width - 40,height:400))
            
    //先创建,在进行位置大小设定,和上面的作用一样的      
    let Label = UILabel()
    Label.frame = CGRect(x:20,y:100,width:self.view.frame.size.width - 40,height:400)
    
    //添加       
    self.view.addSubview(Label);
    

    2、UILabel常见属性设置####

    //显示文本内容
    testLable.text = "nice to meet you!"
    
    //背景颜色
    testLable.backgroundColor = UIColor.green
    
    //文字颜色
    testLable.textColor = UIColor.white
    
    //文字对齐方式
    testLable.textAlignment = NSTextAlignment.left
    
    其中文字对齐方式有以几种:
     /* Values for NSTextAlignment */
     @available(iOS 6.0, *)
     public enum NSTextAlignment : Int {
     
     
     case left // Visually left aligned
     
     
     case center // Visually centered
     
     case right // Visually right aligned
     
     /* !TARGET_OS_IPHONE */
     // Visually right aligned
     // Visually centered
     
     case justified // Fully-justified. The last line in a paragraph is natural-aligned.
     
     case natural // Indicates the default alignment for script
     }
    
    
    //阴影颜色
    testLable.shadowColor = UIColor.black
    
    //阴影偏移位置
    testLable.shadowOffset = CGSize(width:-5,height:5)
    
    //根据视图宽度自动调整文字大小
    testLable.adjustsFontSizeToFitWidth = true
    
    //高亮时候的文字颜色
    testLable.highlightedTextColor = UIColor.cyan
            
    //设置圆角
    testLable.layer.cornerRadius = 20
    testLable.layer.masksToBounds = true
    
    //边框的宽度和颜色
    testLable.layer.borderColor = UIColor.green.cgColor
    testLable.layer.borderWidth = 2
         
    //文字类型/大小   
    testLable.font = UIFont.boldSystemFont(ofSize: 20) //加粗类型
    testLable.font = UIFont.systemFont(ofSize: 20)//文字大小
    testLable.font = UIFont.italicSystemFont(ofSize: 20)//斜体类型
    
    //大小和文字一起设置
    testLable.font = UIFont(name:"您好",size:50)
    
    //显示样式
    testLable.lineBreakMode = NSLineBreakMode.byCharWrapping
    
    // NSParagraphStyle
    @available(iOS 6.0, *)
    public enum NSLineBreakMode : Int {
    
    case byWordWrapping // Wrap at word boundaries, default
    
    case byCharWrapping // Wrap at character boundaries
    
    case byClipping // Simply clip
    
    case byTruncatingHead // Truncate at head of line: "...wxyz"
    
    case byTruncatingTail // Truncate at tail of line: "abcd..."
    
    case byTruncatingMiddle // Truncate middle of line:  "ab...yz"
    }
    
    //多行显示
    label.numberOfLines = 2//最多显示2行
    label.numberOfLines = 0// 默认没有行数显示
    label.numberOfLines = 1//只能显示一行
    
    

    3、富文本设置####

    //创建对象
    let attributeString = NSMutableAttributedString(string:"Welcome to Swift! Welcome to Swift! Welcome to Swift! Welcome to Swift!")
    //设置字体大小/字体类型
    attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0, 6))
    //设置背景颜色           
    attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, 3))          
    //设置文档背景色     attributeString.addAttribute(NSBackgroundColorDocumentAttribute, value: UIColor.lightGray, range: NSMakeRange(10, 10))
    //设置下划线
    attributeString.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(5,12))
            
    testLable.attributedText = attributeString;
    

    4、文本高度计算####

    关于lable显示文本的高度计算也是老生常谈,下面就简单的举个例子以extension为例:

    extension NSString {
        func textSizeWithFont(font: UIFont, constrainedToSize size:CGSize) -> CGSize {
            var textSize:CGSize!
            if CGSizeEqualToSize(size, CGSizeZero) {
                let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)
                textSize = self.sizeWithAttributes(attributes)
            } else {
                let option = NSStringDrawingOptions.UsesLineFragmentOrigin
                let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)
                let stringRect = self.boundingRectWithSize(size, options: option, attributes: attributes, context: nil)
                textSize = stringRect.size
            }
            return textSize
        }
    }
    

    相关文章

      网友评论

        本文标题:Swift—UILabel的常见用法

        本文链接:https://www.haomeiwen.com/subject/xsicnttx.html