Ios Swift 在不改变其他属性的情况下使字体切换粗体、斜体、粗体斜体、普通

Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes

我很惊讶,简单地为现有字体设置粗体和斜体在 Swift 中是如此复杂。

我只是想通过以下字体方法来简化事情 class。

我希望将以下方法添加到已设置字体系列和字体大小的现有字体中。我需要保留这些并仅更改以下内容。

setBold : Shud preserve italic

setItalic : Shud preserve bold

setBoldItalic

setNormal : remove both bold and italic

removeBold : Shud preserve italic

removeitalic : Shud preserve bold

我尝试了下面的方法,使用 fontDescriptorWithSymbolicTraits 对我来说就像一场噩梦。

是否有更简单的方法,只需几行代码即可完成这些操作?

extension UIFont
{
    var isBold: Bool
    {
        return fontDescriptor().symbolicTraits.contains(.TraitBold)
    }

    var isItalic: Bool
    {
        return fontDescriptor().symbolicTraits.contains(.TraitItalic)
    }

    func setBold() -> UIFont
    {
        var fontDescriptorVar: UIFontDescriptor
        if(isBold){
            return self
        }
        else
        {
            fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitBold)
        }
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }

    func setItalic()-> UIFont
    {
        var fontDescriptorVar: UIFontDescriptor
        if(isItalic) {
            return self
        }
        else
        {
            fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitItalic)
        }
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }

    func setBoldItalic()-> UIFont
    {
        let fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(arrayLiteral: .TraitBold, .TraitItalic))
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }

    // Things I need are

    // To set back to normal

    func setNormal()-> UIFont
    {

    }

    // Remove only bold if it's both bold and Italic

    func removeBold()-> UIFont
    {

    }

    // Remove only italic if it's both bold and Italic

    func removeitalic()-> UIFont
    {

    }
}

我不想使用它询问我的大小和字体作为输入:

UIFont(name "namFontFamily", size: 16)

UIFont.systemFontOfSize(16, weight: UIFontWeightLight)

我到处搜索,没有找到符合我需要的简单解决方案。

你说你想保留其他特征,所以你可能需要修改代码中的一些方法:

func setBold() -> UIFont
{
    if isBold {
        return self
    } else {
        var symTraits = fontDescriptor().symbolicTraits
        symTraits.insert([.TraitBold])
        let fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(symTraits)
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }
}

setItalic()setBoldItalic()

所以,removeBold() 应该是这样的:

func removeBold()-> UIFont
{
    if !isBold {
        return self
    } else {
        var symTraits = fontDescriptor().symbolicTraits
        symTraits.remove([.TraitBold])
        let fontDescriptorVar = fontDescriptor().fontDescriptorWithSymbolicTraits(symTraits)
        return UIFont(descriptor: fontDescriptorVar, size: 0)
    }
}

removeItalic() 类似。

但我不确定 setNormal()。您想删除所有特征,还是只想删除斜体和粗体?也许你可以随心所欲地自己做。

这些功能应该在 Swift 中内置,但我希望他们在即将推出的版本中添加它们。

这适用于所有想要在 Swift 中设置粗体和斜体...等的简单解决方案并且不想像我一样度过整个晚上的人。

这具有以下特点:

isBold

isItalic

setBold: Should preserve italic

setItalic: Should preserve bold

setBoldItalic

setNormal: remove both bold and italic

desetBold: Should preserve italic

desetItalic: Should preserve bold

toggleBold

toggleItalic

extension UIFont
{
    var isBold: Bool
    {
        return fontDescriptor().symbolicTraits.contains(.TraitBold)
    }

    var isItalic: Bool
    {
        return fontDescriptor().symbolicTraits.contains(.TraitItalic)
    }

    func setBoldFnc() -> UIFont
    {
        if(isBold)
        {
            return self
        }
        else
        {
            var fontAtrAry = fontDescriptor().symbolicTraits
            fontAtrAry.insert([.TraitBold])
            let fontAtrDetails = fontDescriptor().fontDescriptorWithSymbolicTraits(fontAtrAry)
            return UIFont(descriptor: fontAtrDetails, size: 0)
        }
    }

    func setItalicFnc()-> UIFont
    {
        if(isItalic)
        {
            return self
        }
        else
        {
            var fontAtrAry = fontDescriptor().symbolicTraits
            fontAtrAry.insert([.TraitItalic])
            let fontAtrDetails = fontDescriptor().fontDescriptorWithSymbolicTraits(fontAtrAry)
            return UIFont(descriptor: fontAtrDetails, size: 0)
        }
    }

    func setBoldItalicFnc()-> UIFont
    {
        return setBoldFnc().setItalicFnc()
    }

    func detBoldFnc() -> UIFont
    {
        if(!isBold)
        {
            return self
        }
        else
        {
            var fontAtrAry = fontDescriptor().symbolicTraits
            fontAtrAry.remove([.TraitBold])
            let fontAtrDetails = fontDescriptor().fontDescriptorWithSymbolicTraits(fontAtrAry)
            return UIFont(descriptor: fontAtrDetails, size: 0)
        }
    }

    func detItalicFnc()-> UIFont
    {
        if(!isItalic)
        {
            return self
        }
        else
        {
            var fontAtrAry = fontDescriptor().symbolicTraits
            fontAtrAry.remove([.TraitItalic])
            let fontAtrDetails = fontDescriptor().fontDescriptorWithSymbolicTraits(fontAtrAry)
            return UIFont(descriptor: fontAtrDetails, size: 0)
        }
    }

    func SetNormalFnc()-> UIFont
    {
        return detbBoldFnc().detbItalicFnc()
    }

    func toggleBoldFnc()-> UIFont
    {
        if(isBold)
        {
            return detbBoldFnc()
        }
        else
        {
            return setBoldFnc()
        }
    }

    func toggleItalicFnc()-> UIFont
    {
        if(isItalic)
        {
            return detbItalicFnc()
        }
        else
        {
            return setItalicFnc()
        }
    }
}

SWIFT 3.1

func changeTrait(trait: UIFontDescriptorSymbolicTraits) {
        let range = textView.selectedRange
        let currentAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil)
        guard let currentFont = currentAttributes[NSFontAttributeName] as? UIFont else {
            return
        }

        let fontDescriptor = currentFont.fontDescriptor
        var changedFontDescriptor: UIFontDescriptor!


        if fontDescriptor.symbolicTraits.contains(trait) {
            let existingTraitsWithNewTrait = UIFontDescriptorSymbolicTraits(rawValue: fontDescriptor.symbolicTraits.rawValue & ~trait.rawValue)
            changedFontDescriptor = fontDescriptor.withSymbolicTraits(existingTraitsWithNewTrait)
        } else {
            changedFontDescriptor = fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: fontDescriptor.symbolicTraits.rawValue | trait.rawValue))
        }

        let updatedFont = UIFont(descriptor: changedFontDescriptor , size: 0)

        let newAttributes = [NSFontAttributeName: updatedFont]
        textView.textStorage.beginEditing()
        textView.textStorage.setAttributes(newAttributes, range: range)
        textView.textStorage.endEditing()
 }

调用:

  1. 粗体: changeTrait(trait: .traitBold)
  2. 斜体: changeTrait(trait: .traitItalic)

Swift 3.1

extension UIFont{
var isBold: Bool
{
    return fontDescriptor.symbolicTraits.contains(.traitBold)
}

var isItalic: Bool
{
    return fontDescriptor.symbolicTraits.contains(.traitItalic)
}

func setBold() -> UIFont
{
    if(isBold)
    {
        return self
    }
    else
    {
        var fontAtrAry = fontDescriptor.symbolicTraits
        fontAtrAry.insert([.traitBold])
        let fontAtrDetails = fontDescriptor.withSymbolicTraits(fontAtrAry)
        return UIFont(descriptor: fontAtrDetails!, size: pointSize)
    }
}

func setItalic()-> UIFont
{
    if(isItalic)
    {
        return self
    }
    else
    {
        var fontAtrAry = fontDescriptor.symbolicTraits
        fontAtrAry.insert([.traitItalic])
        let fontAtrDetails = fontDescriptor.withSymbolicTraits(fontAtrAry)
        return UIFont(descriptor: fontAtrDetails!, size: pointSize)
    }
}
func desetBold() -> UIFont
{
    if(!isBold)
    {
        return self
    }
    else
    {
        var fontAtrAry = fontDescriptor.symbolicTraits
        fontAtrAry.remove([.traitBold])
        let fontAtrDetails = fontDescriptor.withSymbolicTraits(fontAtrAry)
        return UIFont(descriptor: fontAtrDetails!, size: pointSize)
    }
}

func desetItalic()-> UIFont
{
    if(!isItalic)
    {
        return self
    }
    else
    {
        var fontAtrAry = fontDescriptor.symbolicTraits
        fontAtrAry.remove([.traitItalic])
        let fontAtrDetails = fontDescriptor.withSymbolicTraits(fontAtrAry)
        return UIFont(descriptor: fontAtrDetails!, size: pointSize)
    }
}
}

不知道能不能帮到你,但我觉得你可以这样做:

extension UIFont {

    public static func CreateWithStyle(name: String, size: CGFloat, styles: [UIFontDescriptor.SymbolicTraits]) -> UIFont {

        let fontDescriptor = UIFontDescriptor(name: name, size: size)
        var fontAtrAry = fontDescriptor.symbolicTraits

        if styles.count > 0 {

            for style in styles {
                fontAtrAry.update(with: style)
            }
        }

        return UIFont(descriptor: fontDescriptor.withSymbolicTraits(fontAtrAry)!, size: size)
    }
}

我不创建删除系统,因为我认为我们不需要它。 当我们创建字体时,我们会建立我们想要的样式,仅此而已。 希望我有所帮助。

另一种方法是检查粗体和斜体按钮的选择

对于粗体:

@IBAction func bold(_ sender : UIButton){
    sender.isSelected = !sender.isSelected
    if sender.isSelected{
        self.textView.font = self.textView.font?.bold()
    }else{
        self.textView.font = self.textView.font?.removeBold()
    }
}

对于斜体:

@IBAction func italic(_ sender : UIButton){
    sender.isSelected = !sender.isSelected
    if sender.isSelected{
        self.textView.font = self.textView.font?.italic()
    }else{
        self.textView.font = self.textView.font?.removeItalic()
    }
}

然后 UIFont 扩展可以包含添加和删除特征的不同方法

确保在 withTraits 方法中添加新特征时保留以前的特征

extension UIFont {

//Add Traits
func withTraits(traits:UIFontDescriptor.SymbolicTraits) -> UIFont {
    let symTraits = fontDescriptor.symbolicTraits
    let descriptor = fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(arrayLiteral: symTraits, traits))
    return UIFont(descriptor: descriptor!, size: 0) //size 0 means keep the size as it is
}

func bold() -> UIFont {
    return withTraits(traits: .traitBold)
}

func italic() -> UIFont {
    return withTraits(traits: .traitItalic)
}

//remove traits
func withoutTraits(traits:UIFontDescriptor.SymbolicTraits) -> UIFont {
    var symTraits = fontDescriptor.symbolicTraits
    symTraits.remove([traits])
    let fontDescriptorVar = fontDescriptor.withSymbolicTraits(symTraits)
    return UIFont(descriptor: fontDescriptorVar!, size: 0)
}

func removeBold() -> UIFont {
    return withoutTraits(traits: .traitBold)
}

func removeItalic() -> UIFont {
    return withoutTraits(traits: .traitItalic)
}

}