Ios 向UILabel添加附加空间以插入图像

Ios 向UILabel添加附加空间以插入图像,ios,objective-c,xcode,uilabel,Ios,Objective C,Xcode,Uilabel,我对在UILabel的文本末尾添加图像有问题。如何检测标签中文本的大小,然后添加额外的空间并将图像插入其中?最好的方法是使用一个UIView 并在此UIView上添加UILabel和UIImageView(根据您的要求提供Fram) 而这UIView到您的UIViewController的MainView的UIView最好的方法是取一个UIView 并在此UIView上添加UILabel和UIImageView(根据您的要求提供Fram) 如果不想使用UIView,请将此UIView添加到UIV

我对在UILabel的文本末尾添加图像有问题。如何检测标签中文本的大小,然后添加额外的空间并将图像插入其中?

最好的方法是使用一个
UIView

并在此
UIView
上添加
UILabel
UIImageView
(根据您的要求提供Fram


而这
UIView
到您的
UIViewController
MainView
UIView

最好的方法是取一个
UIView

并在此
UIView
上添加
UILabel
UIImageView
(根据您的要求提供Fram


如果不想使用UIView,请将此
UIView
添加到
UIViewController的MainView。为什么不检查标签中字符串的长度呢。然后使用frame为图像添加一些额外的空间。[UILabel.text length]返回长度。

如果不想使用UIView。为什么不检查标签中字符串的长度呢。然后使用frame为图像添加一些额外的空间。[UILabel.text length]返回长度。

这里有一个代码段,可以根据特定字体和特定字体大小调整大小。相应地,您可以设置标签的框架,然后设置图像视图

- (CGSize)getSizeFromText:(NSString*)text {

    CGSize constrainedSize;

    constrainedSize.width = give maximum allowable width limit;

    constrainedSize.height = MAXFLOAT;

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:constrainedSize lineBreakMode: UILineBreakModeWordWrap];

    return size;
}

希望这能帮助您解决问题。

下面是一段代码片段,它可以根据特定字体和特定字体大小调整大小。相应地,您可以设置标签的框架,然后设置图像视图

- (CGSize)getSizeFromText:(NSString*)text {

    CGSize constrainedSize;

    constrainedSize.width = give maximum allowable width limit;

    constrainedSize.height = MAXFLOAT;

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:constrainedSize lineBreakMode: UILineBreakModeWordWrap];

    return size;
}

希望这能帮助您解决问题。

NSString
上有一个类别,它返回使用给定字体渲染时字符串的大小。有几种方法,最完整的是
-(CGSize)sizeWithFont:(UIFont*)font minFontSize:(CGFloat)minFontSize实际字体大小:(CGFloat*)宽度实际字体大小:(CGFloat)宽度lineBreakMode:(UILineBreakMode)lineBreakMode
(请参阅)


有了这些信息,您就可以实现您想要做的事情。

NSString
上有一个类别,它返回使用给定字体渲染时字符串的大小。有几种方法,最完整的是
-(CGSize)sizeWithFont:(UIFont*)font minFontSize:(CGFloat)minFontSize实际字体大小:(CGFloat*)宽度实际字体大小:(CGFloat)宽度lineBreakMode:(UILineBreakMode)lineBreakMode
(请参阅)

有了这些信息,你也许能够实现你想做的事情

  • 如果需要,可以创建一些容器UIView,或者只使用superview
  • UILabel
    UIImageView
  • 使用
    [NSString sizeWithFont://code>或类似的
    sizeWith…
    方法计算文本大小
  • 根据文本大小计算
    UIImageView
    的位置
  • 如果需要,可以创建一些容器UIView,或者只使用superview
  • UILabel
    UIImageView
  • 使用
    [NSString sizeWithFont://code>或类似的
    sizeWith…
    方法计算文本大小
  • 根据文本大小计算
    UIImageView
    的位置

  • 我为此编写了一些代码:

    -(void) labelSizeGetter
    {
        UILabel * mylabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    
        float heigth=25.0;//max height value of label.
    
        NSString *yourString = @"My great text";
    
        mylabel.font =[UIFont fontWithName:@"Helvetica-Bold" size:12];
    
        [mylabel setText:yourString];
    
        CGSize s = [mylabel.text sizeWithFont:[UIFont systemFontOfSize:12] 
                            constrainedToSize:CGSizeMake(MAXFLOAT,heigth)
                                lineBreakMode:NSLineBreakByWordWrapping];
    
        // s is a size for text of label. just add 10 pix to width to make it more beautiful.
    
        NSLog(@"%@", NSStringFromCGSize(s));
    
        mylabel.frame=CGRectMake(0, 0, s.width+10, s.height);
    
        [self.view addSubview:mylabel];
    
        UIView * myimage=[[UIView alloc] initWithFrame:CGRectMake(mylabel.frame.origin.x+mylabel.frame.size.width , mylabel.frame.origin.y, 30, 30)];//create your images frame according to frame of your label
    
        myimage.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"arti.png"]];
    
        [self.view addSubview:myimage];
    
    }
    

    我希望它能有所帮助

    我为此编写了一些代码:

    -(void) labelSizeGetter
    {
        UILabel * mylabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    
        float heigth=25.0;//max height value of label.
    
        NSString *yourString = @"My great text";
    
        mylabel.font =[UIFont fontWithName:@"Helvetica-Bold" size:12];
    
        [mylabel setText:yourString];
    
        CGSize s = [mylabel.text sizeWithFont:[UIFont systemFontOfSize:12] 
                            constrainedToSize:CGSizeMake(MAXFLOAT,heigth)
                                lineBreakMode:NSLineBreakByWordWrapping];
    
        // s is a size for text of label. just add 10 pix to width to make it more beautiful.
    
        NSLog(@"%@", NSStringFromCGSize(s));
    
        mylabel.frame=CGRectMake(0, 0, s.width+10, s.height);
    
        [self.view addSubview:mylabel];
    
        UIView * myimage=[[UIView alloc] initWithFrame:CGRectMake(mylabel.frame.origin.x+mylabel.frame.size.width , mylabel.frame.origin.y, 30, 30)];//create your images frame according to frame of your label
    
        myimage.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"arti.png"]];
    
        [self.view addSubview:myimage];
    
    }
    

    我希望它能有所帮助

    我考虑过,但在目前的情况下我不能使用它。我需要在不添加UIView的情况下有额外的空间。我考虑过,但在当前情况下我不能使用它。我需要有额外的空间而不添加UIView。