How to change font style programmatically in objective-c

1.1k views Asked by At

I am working on an iOS app that can edit text. User can change the font style by clicking a UIButton. Basically it change "Bold"

 -(IBAction)boldButton:(id)sender{

if([fontName length] >6){

    if ([[fontName substringFromIndex:[fontName length]-6] isEqual:@"Italic"] ) {

        fontName = [fontName substringToIndex:[fontName length]-7];
    }
}

if ([fontName length] >4) {
    if ([[fontName substringFromIndex:[fontName length]-4] isEqual:@"Bold"] ) {
        fontName = [fontName substringToIndex:[fontName length]-5];

    }else{
        fontName =[NSString stringWithFormat:@"%@-Bold",fontName ];
    }
}
      txtVw.font =  [UIFont fontWithName:fontName size:[[NSNumber numberWithFloat:fontSizeSlider.value]integerValue]];
}

But this is not a ideal solution. I have found that , for the arial font name for italic assign as "ItalicMT".then it is not working for arial and some other fonts. What should be the best way to change font style.

0

There are 0 answers