为单个 class 创建多个文本属性

create multiple text attributes for single class

我是编程新手,我正在尝试创建一个 class 来设置大小、属性、颜色和对齐文本中心,因为我正在使用以下代码。我正在尝试做更多的事情,但是一旦我知道如何设置那个 class.

就可以修改

任何帮助,请。

@implementation OceansTabController
@synthesize textField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textField.text = @"\"The sea, once it casts its spell, holds one in its net of wonder forever.\"\nJacques Yves Cousteau\n\nOur majestic oceans\n\n";

    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:textField.text];
    //
    //    UIFont *font_regular=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];

    UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
    UIFont *font_italic=[UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f];
    UIFont *font_bold=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
    UIFont *font_boldBlue=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
    UIFont *font_boldLarge=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
    UIFont *font_boldLargeCentered=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];

[attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 75)];
[attString addAttribute:NSFontAttributeName value:font_boldBlue range:NSMakeRange(76, 21)];
[attString addAttribute:NSFontAttributeName value:font_boldLargeCentered range:NSMakeRange(98, 20)];

您可以创建 uitextfield 的子class。您可以在其中定义设置属性的方法,如下所示

在customTextField.h

#import <UIKit/UIKit.h>

@interface customtextField : UITextField

- (void)setCustomTextFieldText:(NSString*)text;

- (void)setCustomTextFieldAttributes;

@end

并在 customTextField.m

您可以将字体或任何 属性 定义为 mecros

#import "customtextField.h"

#define font [UIFont fontWithName:@"HelveticaNeue" size:14.0f]
#define font_italic [UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f]
#define font_bold [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]
#define font_boldBlue [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]
#define font_boldLarge [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f]
#define font_boldLargeCentered [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f]

@implementation customtextField

- (void)setCustomTextFieldText:(NSString*)text{
self.text = text;
}

- (void)setCustomTextFieldAttributes{
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:self.text];
[attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 75)];
[attString addAttribute:NSFontAttributeName value:font_boldBlue range:NSMakeRange(76, 21)];
[attString addAttribute:NSFontAttributeName value:font_boldLargeCentered range:NSMakeRange(98, 20)];}

@end

现在您可以将 customTextField 用作 uitextField,并且可以通过在 class 中导入 customTextField.h 文件来设置您需要的所有 属性 :

    customtextField * txtField = [[customtextField alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];

    [txtField setCustomTextFieldText:@"your text for textfield"];
    [txtField setCustomTextFieldAttributes];

    [self.view addSubview:txtField];

肯定对你有用..祝你好运

Shubham bairagi,感谢您的帮助。再次阅读这篇文章,我确实意识到我并不完全清楚我想要什么,因为问题是我不知道所有的条款,无法正确提问。无论如何,我找到了解决方案:

NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:textField.text];
//
//    UIFont *font_regular=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];

UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
UIFont *font_italic=[UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f];
UIFont *font_bold=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
UIFont *font_boldLarge=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];

上面和我发的一样,不过我加了这行是为了设置我想要的颜色

UIColor *blueColor = [UIColor blueColor];

设置变量(希望我使用了正确的术语)

然后我将变量添加到下面的代码中。您会注意到我添加了一行具有相同范围的额外行。这给了我两行具有相同范围的代码(蓝色和粗体)。但是,我发现我没有使用 NSFontAttributeName,而是使用 NSForegroundColorAttributeName 来设置颜色。

[attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 138)];
[attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(139, 21)];
[attString addAttribute:NSForegroundColorAttributeName value:blueColor range:NSMakeRange(139, 21)];
[attString addAttribute:NSFontAttributeName value:font_boldLarge range:NSMakeRange(161, 35)];

如果我不清楚,请告诉我,我会再试一次。

我刚刚想出了如何根据上述格式将文本居中

我创建了变量:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];

并使用此代码添加到与已经加粗的文本相同的范围。

[attString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(161, 35)];