Development Tip

UITextView에 줄 바꿈 추가

yourdevel 2020. 11. 29. 12:20
반응형

UITextView에 줄 바꿈 추가


나는이 UITextView소요 그 NSString서식과를 stringWithUTF8String. 데이터베이스에서 값을 가져오고 데이터베이스의 텍스트가 텍스트 내에서 중단으로 렌더링되기를 원합니다. 나는 \n이것을 사용하려고 시도했지만 텍스트로 렌더링됩니다. 앱의 내 정보 페이지에서 직선 텍스트로이 작업을 수행했지만 데이터베이스에서 가져올 때 작동하지 않는 이유는 서식 때문이라고 생각합니다.

어떤 제안?


Xcode5 / Xcode6 스토리 보드 AI (Attribute-Inspector)에 줄 바꿈을 추가하려면 다음과 같이하십시오.

  1. UITextView를 드래그합니다.
  2. 텍스트를 편집하려면 두 번 클릭하고 단락 나누기를 원하는 위치에 커서를 놓습니다.
  3. 히트 고도는-입력 빈 줄 및 단락을 만들기 위해 몇 번.

또는 CTRL + Return은 새 줄을 만듭니다.

이제 스토리 보드에서 출력 /보기 모드로 효과를 볼 수 있습니다.여기에 이미지 설명 입력


tc의 대답을 약간 확장 : 문자열에 개행 문자가있을 때이를 \n(즉, @"\\n"Cocoa에서) 변환 한 다음 데이터베이스에 저장하고 데이터베이스에서 문자열 BACK을 가져 오면 변환하고 싶을 것입니다. 개행으로 돌아갑니다. 따라서 다음과 같은 코드를 사용합니다.

NSString *saveString = [myTextField.text stringByReplacingOccurrencesOfString: @"\n" withString: @"\\n"];
// save it to the db
// ... later ...
NSString *dbString = // get the string from the db.
myTextField.text = [dbString stringByReplacingOccurrencesOfString: @"\\n" withString: @"\n"];

간단히 아래 :

{
textView.text = @"January: 7th,21st,\n February: 4th,18th,\n March: 4th,18th,\n April: 1st, 15th, 29th, \n May: 13th, 27th, \n June: 10th,24th, \n July: 8th, 22nd, \n August: 5th, 19th, \n September: 2nd, 16th, 30th, \n October: 14th, 28th, \n November: 11th, 25th";
        NSLog (@"Recycle Dates for Abbey Way");

}

textView 또는 선택한 항목에 아래와 같이 출력됩니다.

출력 :

1 월 : 7 일, 21 일,
2 월 : 4 일, 18 일,
3 월 : 4 일, 18 일 등


Swift에서는 두 문자열 사이에 "\ n"을 추가합니다.

예:

let firstString = "Hello"

let secondString = "Senorita"

textview.text = firstString + "\n" + "secondString"

"\ r"을 사용하는 것이 좋습니다.

Xcode 4.2.1, iOS 4.3에서 테스트했습니다.

편집 : iOS 3.0 및 iOS 5.1에서도 작동합니다.


A UITextView will take the newlines, but it will not work with cariage returns. You will have to make sure your input is correct.

I tried it in the IB editor, and had some troubles with it initially. Then I used a text editor, typed my text and then pasted it into the IB editor.

That did the trick, for me. Your source comes from a database, so I think probaly the newlines are actually newlines, but perhaphs carriage return's or other. Check the HEX string of your input.


Set the (UIKeyboardType)keyboardType and (UIReturnKeyTYpe) returnKeyType

if you are building in code.

If you are using IB then just go to the first inspector panel and set the TextInputTraits


when you get from db. try

NSString *content = [mytext stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
self.textView.text = content;

if you are getting the text from web, make sure you put a space before and after the /n that solved my problem

as example (php):

echo $item_detail['item_title']."  \n ";

Olie의 대답 확장 ... Swift에서 데이터베이스의 문자열을 업데이트하는 코드는 다음과 같습니다.

let dbString = // get the string from the db.
myTextField.text = dbString.stringByReplacingOccurrencesOfString("\\\\n", withString: "\n")

알아내는 데 시간이 좀 걸렸으므로 공유 할 것이라고 생각했습니다.


데이터베이스에 문자 "\ n"을 저장하고 있습니다. 개행을 저장하십시오.

참고 URL : https://stackoverflow.com/questions/3697253/adding-a-line-break-to-a-uitextview

반응형