The memo software provided by the iPhone has a good user interface. How does the text line work? I don't know. Please leave a solution.
However, after several days of exploration, I finally come up with a solution. The following is a brief description, which I hope to be clear.
First, I think we need two images. One is the yellow area with no horizontal lines on the editing area, and the other is the yellow area with horizontal lines on the editing area.
The required components are: uiscrollview, uiview, and uitextview.
Stack mode: [uiscrollview addsubview: [uiview addsubview: uitextview];
The background of uiscrollview is set to the first image format, that is
_scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"title.png"]];
_ Scrollview is uiscrollview, and @ "title.png" is the first image.
The uiview background is set to the second image format, that is
_backView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"linemin.png"]];
_ Backlview is uiview and @ "linemin.png" is the second image.
Set the background of uitextview to transparent
The problem is that the uiscollview is hidden by default to slide down to display the cursor.
...
Two problems need to be solved to control the display position of the cursor by yourself. The first is to know the coordinates of the cursor, and the other is how to control uiscollview scrolling first.
It is difficult to calculate the coordinates of the cursor. After several days of learning, I found a method to calculate the coordinates of the cursor.
The following code calculates the Y coordinate of the optical indicator (we only need the Y coordinate, and the X coordinate can be calculated after some supplement, and we can add it by ourselves)
-(INT) getcursorpointy :( uitextview *) curtextview
{
Cgpoint cursorpoint;
Int Len = curtextview. selectedrange. location;
Nsstring * stringtocursor = [curtextview. Text substringtoindex: Len];
Nsarray * partstrarr = [stringtocursor componentsseparatedbystring: @ "\ n"];
Int ROW = [partstrarr count];
Cgsize fontsize = [@ "" sizewithfont: [curtextview font];
For (nsstring * partstr in partstrarr)
{
Cgsize partstrsize = [partstr sizewithfont: [curtextview font] constrainedtosize: curtextview. contentsize linebreakmode: uilinebreakmodecharacterwrap];
Nslog (@ "partstrsize. width = % F, partstrsize. Height = % F", partstrsize. Width, partstrsize. Height );
Int rowg = partstrsize. Height/fontsize. height;
If (rowg> 1 ){
Rowg-= 1;
Row + = rowg;
}
}
Cursorpoint. Y = (Row-1) * fontsize. height;
Return (INT) (cursorpoint. y );
}
At this point, the Y coordinate of the optical indicator can be calculated. In this way, there is still a problem when you use the contentoffset of uiscollview to set the scroll, which will interfere with the method that disappears from the cursor by default.
We can overload the uiscollview's-(void) scrollrecttovisible :( cgrect) rect animated :( bool) animated; Method to shield the method that disappears from the cursor by default, and add your own control method.
By now, a complete solution has been created.
We are actively thinking about whether there are any better solutions.
By Mac-z