Ios Swift making font toggle bold, italic, boldItalic, normal without change other attributes
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.
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()
}
}
}
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)
}
}
}