在UITextView中重置段落间距

时间:2013-09-18 08:47:54

标签: ios objective-c uitextview nsattributedstring spacing

这是我在我的UITextView中添加paragraphSpacing的代码,但是如何将其删除?

尝试paragraphStyle.paragraphSpacing = 0;,但不会改变。

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

1 个答案:

答案 0 :(得分:0)

我所做的是使用UITextView创建一个视图并添加两个按钮。一个针对您的功能并成功添加了parapraph间距。另一个针对以下功能并成功删除了间距。在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;

}