0

这是我在我的中添加段落间距的代码UITextView,但是如何删除它?

试过了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;
}
4

1 回答 1

0

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

}
于 2014-09-08T19:02:04.780 回答