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

uilabel italic font programmatically swift
nsattributedstring italic swift
bold non bold text in a single uilabel
uilabel bold font programmatically
swift change font weight
set a default font for whole ios app swift
change font style programmatically swift
uifontdescriptor

I am surprised, that simply setting bold and italic for existing font is so complicated in Swift.

I just want to simplify things by having following methods on font class.

I want the below methods to be added to existing font which has font-family and font-size set. I need to preserve these and change only the following.

setBold : Shud preserve italic

setItalic : Shud preserve bold

setBoldItalic

setNormal : remove both bold and italic

removeBold : Shud preserve italic

removeitalic : Shud preserve bold

I tried the below, and it's feeling like a nightmare for me using fontDescriptorWithSymbolicTraits.

Is there a simpler way of doing these in a few lines of code?

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
    {

    }
}

I don't want to use this which asks me the size and font as input:

UIFont(name "namFontFamily", size: 16)

UIFont.systemFontOfSize(16, weight: UIFontWeightLight)

I searched everywhere and found no simple solution matching my needs.

You say you want to preserve other traits, so you may need to modify some methods in your code:

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() and setBoldItalic() as well.

So, removeBold() should be something like this:

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() would be similar.

But I'm not sure about setNormal(). You want remove all traits, or want to just remove italic and bold? Maybe you can do it yourself as you like.

Extending UIFont to Make Fonts Bold or Italic in Swift, This extension for UIFont in Swift will convert your font to bold or italic. you don't need this extension to make a regular system font bold or italic. The size parameter is set to zero, which means don't change the font size� [ Natty] ios Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes By: Saleh Altahini 1.0; [ Natty ] windows Windows Store and All Apps Crash Immediately After Opening By: Elderwolf123 5.5 ;

These functions should have come inbuilt in Swift, but I wish they add them in upcoming versions.

This is for all who wants a simple solution for setting bold and italic... etc. in Swift and don't want to spend the whole night like me.

This has following features:

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()
        }
    }
}

UIFont fontDescriptor trait with size variant, 标题:Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes. 作者:Sujay U N. 发表时间:2016-07-22 18:41:02. There may be up to three ways to specify type style. Simple Tuple Fonts. As a tuple whose first element is the font family, followed by a size in points, optionally followed by a string containing one or more of the style modifiers bold, italic, underline and overstrike.

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)
    }
}
}

User Sujay U N, traitItalic) } var boldItalic: UIFont { return with(traits: [.traitBold, .traitItalic]) } func of the UILabel and other UIViews: UILabel.appearance().font = UIFont(name: fontAttributes return text.size(withAttributes: attributes).width } Swift 4 Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes. How to change default font on Windows 10. To change the system font on Windows 10, use these steps: Warning: This is a friendly reminder that editing the Registry is risky, and it can cause

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()
 }

INVOKE:

  1. FOR BOLD: changeTrait(trait: .traitBold)
  2. FOR ITALIC: changeTrait(trait: .traitItalic)

How do I set bold and italic on UILabel of iPhone/iPad?, Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes � stackoverflow.com � 7 � Adding UIBarButton item in swift. HTML tags are easy solutions for simple problems, like making a text bold, italic, or even displaying bullet points. To style text containing HTML tags, call Html.fromHtml method. Under the hood

I don't know if I can help you, but I think you can do this way:

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)
    }
}

I don't create a remove system because I don't think we would need it. When we are creating a Font, we establish the styles we want and that's it. Hope I helped.

Swift italic font, Select the label you want to edit, go to it's attribute inspector. Ios Swift making font toggle bold, italic, boldItalic, normal without , You say you want to preserve other traits, so you may need to modify some methods in your code: func setBold ()� Typefaces are defined by specifying a font's family and style. Conventional styles are usually limited to a small set of orthogonal attributes. Typical styles include bold, italic, bold italic, underline, shadow, condensed, and extended. Currently, most font families contain one to four styles.

Text in PDFKit, Download Swift Regular Italic, font family Swift by with Regular Italic weight and style, Our Other Services Include Fonts Heartz font download for free, in ttf for The most widely used and bundled Arial fonts are Arial Regular, Italic, Bold, and attribute with “italic” in XML layout file or change the text style dynamically in� The type of the font, such as “Bold”, “Italic”, “Roman”, “Light”, etc. The font’s weight as an integer. The font’s traits as an unsigned integer (UInt). An item-array is called a member of the font family. The following example taken from Apple Documentation illustrates what availableMembers(ofFontFamily:) returns:

ios, PDFKit makes adding text to documents quite simple, and includes many options to However, you can modify the position of text by passing X and Y coordinates to the this includes 4 styles (regular, bold, italic/oblique, bold+italic) of Helvetica, The PDF format also allows fonts to be embedded right in the document. Make sure the menu items within a submenu are logically related. In general, submenus work best for menu items that toggle attributes on and off rather than initiate actions. For example, the Format menu in Pages has a Font submenu that includes menu items for enabling and disabling text attributes, such as Bold, Italic, and Underline.

in Swift, Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes. 发表于 2016-07-22 18:41:02. 活跃于 2019-04-18 11:06:02. 查看9275 次. Only italic and normal are well supported by most browsers and fonts. The font-style property indicates if the font should be italic, oblique, or normal. Click your Start Button, type wordpad and hit enter. The possible text styles are bold, italic and normal. Get the best deals for samsung galaxy note 10 plus at eBay.

Comments
  • I think it's really important to check the font family because not all of them have bold and italic forms, are you using default fonts or did you add a new one?
  • I have my own list of font family which has bold and italic. Also Text Color is also there with the font
  • most of the fonts have "Bold" or "Italic" suffix so if your font names matching this pattern maybe you will construct your fonts by their names.
  • My methods shud be irrespective of font family, It shud be bolded like in css. Because it am trying to create a general one for all time use
  • This is what i was looking for, Thanks a lot. You really saved my day :-)
  • @ReimondHill, Please explain not working. As far as I test my code with Xcode 10 and iPhone sim 12.0, my code translated for Swift 4.2 works as expected.