Ios 在特定位置分配UITextView
我在屏幕的中心有一个圆圈,我正在围绕这个固定的圆圈(UIImageView)分配一系列UILabel。标签的数量由NSMutableArray中的元素数量给出,标签的位置取决于标签的数量。我不能给标签指定一个固定的x和y坐标,因为标签的数量会有所不同 我尝试使用以下代码:Ios 在特定位置分配UITextView,ios,xcode,Ios,Xcode,我在屏幕的中心有一个圆圈,我正在围绕这个固定的圆圈(UIImageView)分配一系列UILabel。标签的数量由NSMutableArray中的元素数量给出,标签的位置取决于标签的数量。我不能给标签指定一个固定的x和y坐标,因为标签的数量会有所不同 我尝试使用以下代码: - (void)loadContent{ NSString *filename6 = [[NSUserDefaults standardUserDefaults]objectForKey:@"Setting"]; NSAr
- (void)loadContent{
NSString *filename6 = [[NSUserDefaults standardUserDefaults]objectForKey:@"Setting"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *groupPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", filename6]];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:groupPath];
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:[fileContent componentsSeparatedByString:@", "]];
int arrayCount = array.count;
int yCoordinate = (2*M_PI) / arrayCount;
int xCoordinate = ;
for(int i = 0; i < [array count]; i++){
CGRect textframe = CGRectMake( the xcoordinate, the ycoordinate, 328, 30);
NSString *nameOfGroup = [array objectAtIndex:i];
UITextView* theGroupTextLabel;
theGroupTextLabel = [[UITextView alloc] initWithFrame: textframe];
[theGroupTextLabel setText: nameOfGroup];
[theGroupTextLabel setTextColor: [UIColor redColor]];
[self.view addSubview:theGroupTextLabel];
//theGroupTextLabel.enabled = NO;
theGroupTextLabel.backgroundColor = [UIColor clearColor];
theGroupTextLabel.layer.borderWidth = 3.5f;
theGroupTextLabel.layer.borderColor = [[UIColor blackColor] CGColor];
int z;
z = z + 1;
theGroupTextLabel.tag = z;
}
}
…坐标。有什么想法吗?这是我正在使用的正确方法吗?这更像是一个数学问题,而不是一个代码问题,但我要尝试一下 假设您希望第一个项目位于顶部,然后围绕主图像顺时针放置项目,那么在遍历for循环时将更改一个变量:角度 然后,通过将角度和半径(可能是常数)转换为笛卡尔坐标,并将结果添加到图像中心的坐标,计算每次迭代中的x和y坐标。这需要在for循环中完成,而不是在代码的开头 每次循环迭代时,将添加到角度的量为
2*M_PI/arrayCount
这大概就是我的想法:
- (void)loadContent{
NSString *filename6 = [[NSUserDefaults standardUserDefaults]objectForKey:@"Setting"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *groupPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", filename6]];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:groupPath];
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:[fileContent componentsSeparatedByString:@", "]];
int arrayCount = array.count;
CGFloat radius = 300;// <--- INSERT RADIUS HERE
CGFloat angle = 0;// <--- The starting angle
CGPoint center = CGPointMake(300,300); // <--- INSERT CENTER OF ARRANGEMENT
for(int i = 0; i < [array count]; i++){
int yCoordinate = radius * cos(angle) + center.y;
int xCoordinate = radius * sin(angle) + center.x;
CGRect textframe = CGRectMake( the xcoordinate, the ycoordinate, 328, 30);
NSString *nameOfGroup = [array objectAtIndex:i];
UITextView* theGroupTextLabel;
theGroupTextLabel = [[UITextView alloc] initWithFrame: textframe];
[theGroupTextLabel setText: nameOfGroup];
[theGroupTextLabel setTextColor: [UIColor redColor]];
[self.view addSubview:theGroupTextLabel];
//theGroupTextLabel.enabled = NO;
theGroupTextLabel.backgroundColor = [UIColor clearColor];
theGroupTextLabel.layer.borderWidth = 3.5f;
theGroupTextLabel.layer.borderColor = [[UIColor blackColor] CGColor];
int z;
z = z + 1;
theGroupTextLabel.tag = z;
// INCREMENT ANGLE
angle += 2 * M_PI / array.count;
}
}
-(无效)加载内容{
NSString*filename6=[[NSUserDefaults standardUserDefaults]objectForKey:@“设置”];
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*groupPath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@“%@”,filename6]];
NSString*fileContent=[[NSString alloc]initWithContentsOfFile:groupPath];
NSMutableArray*array=[[NSMutableArray alloc]initWithArray:[fileContent componentsSeparatedByString:@“,”];
int arrayCount=array.count;
CGFloat radius=300;//处理圆时,X和Y坐标是(cos角,sin角)
,因此,为了计算X和Y坐标,您可能需要执行以下操作
float angle = (2*M_PI) / arrayCount;
int xCoordinate = (cos(angle * i) * circleRadius) + circleCenterX;
int yCoordinate = (sin(angle * i) * circleRadius) + circleCenterY;
这将给出圆上标签应出现的点
N.B.您需要将角度
乘以i
,以便将下一个标签移动到正确的位置。在计算角度时,您可能还需要将数组计数加1,以防止最后一个标签与第一个标签重叠
来源:这些标签是如何定位的?是在网格中?还是在图像周围的圆圈中(如时钟表面的数字)?我注意到你有一个M_PI在里面,所以我猜它像一个时钟?图像周围显示一个圆圈,是的,像一个时钟。这个代码很棒,但是你能简单地解释一下排列中心和“插入半径”是什么吗?中心点是您希望项目辐射的位置,通常是大圆图像中心的坐标。半径是从圆心到其边缘的距离,这可能是大圆图像长度的一半。我在代码中没有看到对该图像的引用,否则我会我刚用过,所以你得自己填这些数字。
float angle = (2*M_PI) / arrayCount;
int xCoordinate = (cos(angle * i) * circleRadius) + circleCenterX;
int yCoordinate = (sin(angle * i) * circleRadius) + circleCenterY;