xcode 如何绘制圆角矩形标签或视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7120308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:45:58  来源:igfitidea点击:

How can I draw a rounded rectangle Label or View?

iphoneobjective-cxcode

提问by Oksana

I need to draw a rounded rectangle bar in my iOS application, but without using a background image. Is there any way to make a rounded rectangle view or label?

我需要在我的 iOS 应用程序中绘制一个圆角矩形条,但不使用背景图像。有没有办法制作圆角矩形视图或标签?

回答by Mundi

In addition to the other answer(s), don't forget to set the masksToBoundsproperty.

除了其他答案之外,不要忘记设置masksToBounds属性。

#import <QuartzCore/QuartzCore.h>

label.layer.cornerRadius = 5.0;
label.layer.masksToBounds = YES;

回答by Jonathon Horsman

Alternative if you're using the UI designer (Storyboard or nib file) you can set a User Defined Runtime Attribute.

或者,如果您使用 UI 设计器(故事板或 nib 文件),您可以设置用户定义的运行时属性。

Click on the view you want to have rounded corners, click Show the Identity Inspector (3rd tab, top right). Then click the + in User Defined Runtime Attributes and enter the following:

单击您想要圆角的视图,单击显示身份检查器(第三个选项卡,右上角)。然后单击 User Defined Runtime Attributes 中的 + 并输入以下内容:

Key Path: layer.cornerRadius
Type: Number
Value: whatever number, e.g. 5

回答by Ravi Chokshi

Add Quartz core Framework ..

添加 Quartz 核心框架 ..

#import <QuartzCore/QuartzCore.h>

Then Set corner radius,

然后设置圆角半径,

yourView_LabelName.layer.cornerRadius = 10.0;

回答by Tendulkar

It may be useful for you. Step1 :add the quartzcore framework to your project frameworks. In which file you want to write this code there, you have to use this.

它可能对你有用。步骤1:将quartzcore 框架添加到您的项目框架中。你想在哪个文件里写这段代码,你必须使用这个。

        #import <QuartzCore/QuartzCore.h>
        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 50, 30)];
        myLabel.text = @"text";
        myLabel.layer.cornerRadius =8.0;
        [self.view addSubview:myLabel];