将属性标签设置为导航标题

时间:2018-04-26 10:00:29

标签: ios uinavigationcontroller uilabel

我正在使用swift 4处理IOS。我必须制作导航控制器,其左侧是带有Image的中心对齐应用程序标题。但我没有得到如何将图像附加到左侧。归因于Image总是向右移动。

为此,我将字符串附加两次。下面是代码

   //Get image and set it's size
    let image = UIImage(named: "user2")
    let newSize = CGSize(width: 30, height: 30)

    //Resize image
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image?.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
    let imageResized = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //Create attachment text with image
    let attachment = NSTextAttachment()
    attachment.image = imageResized
    let attachmentString = NSAttributedString(attachment: attachment)
    let myString = NSMutableAttributedString(string: "")
    myString.append(attachmentString)
    let strAttachment = NSAttributedString(string: "My Ios App" )
    myString.append(strAttachment)

    navLabel.attributedText = myString

    self.navigationItem.titleView = navLabel

我得到的是左下方的图像 enter image description here

我想要的是什么:

我只想用图像和图像之间的空间显示图像。 我希望图像看起来很好看。文本应垂直居中于图像。实现这个目标的好方法是什么?

2 个答案:

答案 0 :(得分:1)

如果您想调整附件位置,则必须使用attachment.bounds属性,如果要调整文本垂直对齐方式,则需要使用NSAttributedString baselineOffset

<强>更新

Swift 3代码

        let normalNameString = NSMutableAttributedString.init(string: "")

        let attachment = NSTextAttachment()
        attachment.image = imageHelper.pgImage(textValue: "PG-13")
        attachment.bounds = CGRect(x: 0, y: 0, width: (attachment.image?.size.width)!, height: (attachment.image?.size.height)!)
        normalNameString.append(NSAttributedString(attachment: attachment))
        normalNameString.append(NSAttributedString(string: " My Ios App", attributes: [NSBaselineOffsetAttributeName :6]))
        self.lblText.attributedText = normalNameString

Swift 4代码

        let normalNameString = NSMutableAttributedString.init(string: "")

        let attachment = NSTextAttachment()
        attachment.image = imageHelper.pgImage(textValue: "PG-13")
        attachment.bounds = CGRect(x: 0, y: 0, width: (attachment.image?.size.width)!, height: (attachment.image?.size.height)!)
        normalNameString.append(NSAttributedString(attachment: attachment))
        normalNameString.append(NSAttributedString(string: " My Ios App", attributes: [NSAttributedStringKey.baselineOffset :6]))
        self.lblText.attributedText = normalNameString

enter image description here

答案 1 :(得分:1)

使用此代码,您将获得输出:

        let navgationView = UIView()

        let label = UILabel()
        label.text = " My Ios App"
        label.sizeToFit()
        label.center = navgationView.center
        label.textAlignment = NSTextAlignment.center

        let image = UIImageView()
        image.image = UIImage(named: "user")
        let imageAspect = image.image!.size.width/image.image!.size.height
        image.frame = CGRect(x: label.frame.origin.x-label.frame.size.height*imageAspect, y: label.frame.origin.y, width: label.frame.size.height*imageAspect, height: label.frame.size.height)
        image.contentMode = UIViewContentMode.scaleAspectFit

        navgationView.addSubview(label)
        navgationView.addSubview(image)

        self.navigationItem.titleView = navgationView
        navgationView.sizeToFit()

输出: -

enter image description here

希望它有所帮助。!!