简体   繁体   中英

iOS - How to draw a circle with a number/character in it?

I would like to create a UIView which would show a colored circle with a number or character in it. I have seen examples to create a circle, but how would I accomplish this?

I would want to place multiple of these views on the screen and connect them with lines.

Any suggestions?

// Swift, iOS8
var letterLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 100, height: 100))
letterLabel.layer.cornerRadius = 50
letterLabel.textAlignment = NSTextAlignment.Center
letterLabel.layer.borderWidth = 10
letterLabel.layer.backgroundColor = UIColor.blueColor().CGColor
letterLabel.font = UIFont(name: "Hoefler Text", size:75.0)
letterLabel.text = "A"

在此输入图像描述

If you already know how to draw a circle, then you've got half of it solved :) Let's say the circle is drawn in a UIView called circleView .

Now you want to create a UILabel to draw the letter in.

UILabel* circleLabel = [[UILabel alloc] init];
[circleLabel setText:@"letter goes here"];

And now you want to put the label on top of the circleView , so you add it as a subview:

[circleView addSubview:circleLabel];

At this point, depending on the size of circleView and the font size of circleLabel , the label may or may not be in the center of the circle. Use the setFrame function on the circleLabel to adjust its position within the circleView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM