简体   繁体   中英

Reset Paragraph Spacing in UITextView

This is my Code to add paragraphSpacing in my UITextView , but how to remove it?

Tried paragraphStyle.paragraphSpacing = 0; , but it does not change.

- (IBAction)addParagraphSpacing:(id)sender {

    UITextView *selectedtextfield = (UITextView *)[self.view viewWithTag:1];

    // get font name & pointsize
    UIFont *currentfontname = [selectedtextfield font];
    NSString *currentFontname = [currentfontname fontName];

    UIFont *currentfontsize = [selectedtextfield font];
    CGFloat currentfontSize = [currentfontsize pointSize];

    CGFloat NewParagraphSpacing = currentfontSize / 2;

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.paragraphSpacing = NewParagraphSpacing;

    NSString *string = selectedtextfield.text;

    UIFont *currenttypo = [UIFont fontWithName:@"Arial" size:currentfontSize]; // ?
    NSDictionary *attributtes = @{ NSParagraphStyleAttributeName : paragraphStyle,};
    selectedtextfield.attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributtes];
    selectedtextfield.font = currenttypo;

    selectedtextfield.font = [UIFont fontWithName:currentFontname size:currentfontSize];
    selectedtextfield.textAlignment = NSTextAlignmentCenter;
}

What I did was to create a single view with a UITextView and added two buttons. One targeted your function and added successfully the parapraph spacing. The other one targeted the following function and removed successfully the spacing. Tested in iOS 7.1:

- (IBAction)removeParagraphSpacing:(id)sender {

    UITextView *selectedtextfield = (UITextView *)[self.view viewWithTag:1];

    // get font name & pointsize
    UIFont *currentfontname = [selectedtextfield font];
    NSString *currentFontname = [currentfontname fontName];

    UIFont *currentfontsize = [selectedtextfield font];
    CGFloat currentfontSize = [currentfontsize pointSize];

    CGFloat NewParagraphSpacing = 0.0f;

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.paragraphSpacing = NewParagraphSpacing;

    NSString *string = selectedtextfield.text;

    UIFont *currenttypo = [UIFont fontWithName:@"Arial" size:currentfontSize]; // ?
    NSDictionary *attributtes = @{ NSParagraphStyleAttributeName : paragraphStyle};
    selectedtextfield.attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributtes];
    selectedtextfield.font = currenttypo;

    selectedtextfield.font = [UIFont fontWithName:currentFontname size:currentfontSize];
    selectedtextfield.textAlignment = NSTextAlignmentCenter;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM